如何为 Android 应用创建透明的演示屏幕?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12013334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:27:29  来源:igfitidea点击:

How do you create a transparent demo screen for an Android app?

androidandroid-viewpagertransparentdemoshowcaseview

提问by Gautam

I'm trying to create a semi-transparent demo screen that is launched only when a user first installs my application. Here's an example from the Pulse News app:

我正在尝试创建一个仅在用户首次安装我的应用程序时启动的半透明演示屏幕。以下是 Pulse News 应用中的示例:

Galaxy Nexus

银河系

Example screenshot from Pulse News on Galaxy Nexus

Galaxy Nexus 上 Pulse News 的示例截图

Nexus One

连结一

enter image description here

在此处输入图片说明

Instead of a 'tap-to-dismiss' feature, I want the user to be able to swipe through a couple of such transparent demo pages.

我希望用户能够浏览几个这样的透明演示页面,而不是“点击关闭”功能。

For my first attempt, I modified a sample from the ViewPagerIndicatorlibrary. I used semi-transparent PNGs in ImageViews inside each of the view pager's fragments. I then launched this as a 'demo activity' in the onCreate method of my 'main activity'.

对于我的第一次尝试,我修改了ViewPagerIndicator库中的一个示例。我在每个视图寻呼机片段内的 ImageViews 中使用了半透明的 PNG。然后,我在“主要活动”的 onCreate 方法中将此作为“演示活动”启动。

Problem: The 'main activity' could not be seen in the background - instead it was just black. I tried the solutions here, but that didn't fix the problem.

问题:在背景中无法看到“主要活动” - 相反,它只是黑色的。我在这里尝试了解决方案,但这并没有解决问题。

Is there a better approach to creating something like this, or am I on the right track?

有没有更好的方法来创建这样的东西,还是我在正确的轨道上?

I also had another related question which depends on how this is implemented. I'm trying to overlay text and arrows such that they point at particular UI components in the background. By using a PNG that has the text and arrows, it's likely that it will not scale properly on different devices. I.e., the arrows may not necessarily point to the correct UI component in the background. Is there a way to tackle this problem as well?

我还有另一个相关的问题,这取决于这是如何实现的。我正在尝试覆盖文本和箭头,以便它们指向背景中的特定 UI 组件。通过使用具有文本和箭头的 PNG,它可能无法在不同设备上正确缩放。即,箭头可能不一定指向后台的正确 UI 组件。有没有办法解决这个问题?

Thanks!

谢谢!

Here's my code for the first attempt:

这是我第一次尝试的代码:

DemoActivity.java

演示活动.java

public class DemoActivity extends FragmentActivity {
    DemoFragmentAdapter mAdapter;
    ViewPager mPager;
    PageIndicator mIndicator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_activity);

        mAdapter = new DemoFragmentAdapter(getSupportFragmentManager());

        mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);
        //mPager.setAlpha(0);

        UnderlinePageIndicator indicator = (UnderlinePageIndicator)findViewById(R.id.indicator);
        indicator.setViewPager(mPager);
        indicator.setFades(false);
        mIndicator = indicator;
    }

}

DemoFragmentAdapter.java

DemoFragmentAdapter.java

class DemoFragmentAdapter extends FragmentPagerAdapter {
    protected static final int[] CONTENT = new int[] { R.drawable.demo1, R.drawable.demo2, R.drawable.demo3, R.drawable.demo4};

    private int mCount = CONTENT.length;

    public DemoFragmentAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return DemoFragment.newInstance(CONTENT[position % CONTENT.length]);
    }

    @Override
    public int getCount() {
        return mCount;
    }

    public void setCount(int count) {
        if (count > 0 && count <= 10) {
            mCount = count;
            notifyDataSetChanged();
        }
    } }

DemoFragment.java

DemoFragment.java

public final class DemoFragment extends Fragment {
    private static final String KEY_CONTENT = "TestFragment:Content";

    public static DemoFragment newInstance(int content) {
        DemoFragment fragment = new DemoFragment();
        fragment.mContent = content;
        return fragment;
    }

    private int mContent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
            mContent = savedInstanceState.getInt(KEY_CONTENT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        ImageView image = new ImageView(getActivity());
        image.setBackgroundResource(mContent);

        LinearLayout layout = new LinearLayout(getActivity());
        layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        layout.setGravity(Gravity.CENTER);
        layout.addView(image);

        return layout;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY_CONTENT, mContent);
    }
}

采纳答案by Benito Bertoli

Put your demo info in a different activity and give it the following theme.

将您的演示信息放在不同的活动中,并为其指定以下主题。

<style name="Transparent" parent="@android:style/Theme.NoTitleBar">
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>      
    <item name="android:backgroundDimEnabled">false</item>
</style>

If you're using ActionBarSherlock change parentto @style/Theme.Sherlock.

如果您使用 ActionBarSherlock,请更改parent@style/Theme.Sherlock.

This will give you a transparent activity, so you will be able to see the activity below it.

这将为您提供一个透明的活动,因此您将能够看到其下方的活动。

Now I'm guessing you want a translucent background too.

现在我猜你也想要一个半透明的背景。

In the xml layout (of your transparent activity) add:

在 xml 布局(您的透明活动)中添加:

android:background="#aa000000" 

The last 6 digits define the color: 000000 is black.

最后 6 位数字定义颜色:000000 是黑色。

The first 2 define the opacity: 00 is 100% transparent, ff is 100% opaque. So choose something in between.

前 2 个定义不透明度:00 是 100% 透明,ff 是 100% 不透明。所以选择介于两者之间的东西。

回答by Nik

Have you looked at ShowcaseView? https://github.com/Espiandev/ShowcaseView.

你看过 ShowcaseView 吗?https://github.com/Espiandev/ShowcaseView

Using this:

使用这个:

View showcasedView = findViewById(R.id.view_to_showcase);
ViewTarget target = new ViewTarget(showcasedView);
ShowcaseView.insertShowcaseView(target, this, R.string.showcase_title, R.string.showcase_details);

回答by powder366

Pulse is using a RelativeLayout with four ImageView's and four TextView's. The text in the screen shot is all TextView's with their own custom font.

Pulse 正在使用具有四个 ImageView 和四个 TextView 的 RelativeLayout。屏幕截图中的文本都是带有自己自定义字体的 TextView。

In your Manifest add the following to your Activity:

在您的清单中,将以下内容添加到您的活动中:

android:theme="@style/Theme.Transparent">

android:theme="@style/Theme.Transparent">

In to your outer RelativeLayout add:

在您的外部 RelativeLayout 添加:

android:background="#aa000000"

机器人:背景=“#aa000000”

To your styles.xml file:

到你的styles.xml 文件:

<style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>    

An example how to program the custom font you can find at:

您可以在以下位置找到如何编程自定义字体的示例:

https://github.com/commonsguy/cw-android/tree/master/Fonts/FontSampler/

https://github.com/commonsguy/cw-android/tree/master/Fonts/FontSampler/

The layout from the Hierarchy Viewer looks like this (the red box is the RelativeLayout container):

Hierarchy Viewer 中的布局如下所示(红色框是 RelativeLayout 容器):

enter image description here

在此处输入图片说明

回答by Ravikiran

setContentView(R.layout.sample_main);
showOverLay();

private void showOverLay(){

    final Dialog dialog = new Dialog(context, android.R.style.Theme_Translucent_NoTitleBar);

    dialog.setContentView(R.layout.transparent);

    RelativeLayout layout = (RelativeLayout) dialog.findViewById(R.id.transparent);

    layout.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View arg0) {

            dialog.dismiss();

        }

    });

    dialog.show();

}

回答by Surendar D

For this you need to create help layout in bottom of your main layout ex:(structure)

为此,您需要在主布局的底部创建帮助布局,例如:(结构)

<Parent layout>

<Layout 1>(Linear,Relative,....)
  Main layout
  your view...
</Layout 1>

<Layout help>
  set #70000000 as background of this layout 
  #70(transparent range can change) 000000(black)
  and height and width as fillparent
</Layout help>

</Parent layout>

回答by Eric

Wrap your main layout in a RelativeLayout, then add a second layout to that, something like:

将您的主要布局包装在 a 中RelativeLayout,然后添加第二个布局,例如:

<RelativeLayout
    .... >

    <LinearLayout
        .... >

        <!-- Contents of your main layout -->

    </LinearLayout>

    <LinearLayout
        ....
        android:background="#44000000" > <!-- This is alpha 68/255, black -->

        <!-- Contents of your overlay layout -->

    </LinearLayout>

</RelativeLayout>

I believe the overlay layout goes below the main layout in the XML file (if memory serves). You can then make your own layout, ViewFlipper, whatever you want within this second layout.

我相信覆盖布局低于 XML 文件中的主要布局(如果没记错的话)。然后,您可以ViewFlipper在第二个布局中创建自己的布局,无论您想要什么。

回答by Ankush Wadhwa

Make a new Activity (say Tutorial).

制作一个新活动(比如教程)。

Go to your Activity's layout xml file (activity_tutorial). Under the parent layout, add "android:background= #000" and "android:alpha= "0.5"

转到您的活动的布局 xml 文件 (activity_tutorial)。在父布局下,添加“android:background=#000”和“android:alpha=”0.5”

<RelativeLayout 
    
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
                
                
    tools:context=".Tutorial_Activity" 
    android:background="#000"
    android:alpha="0.5">
  ..................................................
  ....................................................
  .............................................
  ...............................................
  
  </RelativeLayout>

Now, go to application manifest file and under your tutorial activity add attribute android:theme="@android:style/Theme.Translucent.NoTitleBar">

现在,转到应用程序清单文件并在您的教程活动下添加属性 android:theme="@android:style/Theme.Translucent.NoTitleBar">

<application>
 .........................................
..........................................
....................................
..........................................

<activity
            android:name="com.aird.airdictionary.Tutorial_Activity"
            android:label="@string/title_activity_tutorial"
            
          android:theme="@android:style/Theme.Translucent.NoTitleBar">
  
        </activity>
    </application>

</manifest>

Thats it, now run this activity on top of any other activity and you can get the desired results. Customize, add text, imageviews and other stuff to get your desired tutorial screen. Pretty sure that you can make a viewpager work with this technique.

就是这样,现在在任何其他活动之上运行此活动,您可以获得所需的结果。自定义、添加文本、图像视图和其他内容以获得所需的教程屏幕。非常确定您可以使用这种技术使 viewpager 工作。

回答by Jug6ernaut

You could just check out the Android launcher code, as they do it. I do not know there implementation.

您可以查看 Android 启动器代码,因为他们这样做。不知道有没有执行。

If it was me I would (if just a simple overlay) so you dont screw with your layout for your application, just create your overlay layout, and attach it over ur application layout by adding it directly with your activities WindowManager. Could be as simple as adding a ImageViewto the WindowManager, listen for touches on the ImageView, or have a timeout to to remove the ImageViewfrom your Window.

如果是我,我会(如果只是一个简单的覆盖)所以你不会为你的应用程序搞砸你的布局,只需创建你的覆盖布局,并通过直接将它添加到你的活动中来将它附加到你的应用程序布局上WindowManager。可能是那样简单添加ImageViewWindowManager,听上触摸ImageView,或有超时删除ImageViewWindow