如何创建一个透明的演示屏幕的Andr​​oid应用程序?创建一个、应用程序、演示、屏幕

2023-09-11 12:32:21 作者:无心者、

我试图创建一个推出仅当用户第一次安装我的应用程序半透明的演示画面。下面是脉冲新闻应用程序的一个例子:

Galaxy Nexus的

的Nexus One

而不是一个点触辞退功能,我希望用户能够通过刷卡一对夫妇这样的透明的演示页面。

有关我的第一次尝试,我修改从 ViewPagerIndicator 库中的样本。我用半透明的PNG在ImageViews里面每个视图寻呼机的片段。然后,我推出了本作中,我的主要活动的onCreate方法一示范活动。

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

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

我也有另外一个相关的问题取决于这是如何实现的。我试图覆盖文字和箭头,这样它们在后台特别是UI组件点。通过使用PNG具有文字和箭头,很可能将无法正常比例在不同的设备。即,箭头不一定指向在后台正确UI组件。有没有办法来解决这个问题呢?

谢谢!

下面是我的code的第一次尝试:

DemoActivity.java

 公共类DemoActivity扩展FragmentActivity {
    DemoFragmentAdapter mAdapter;
    ViewPager mPager;
    PageIndicator mIndicator;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.demo_activity);

        mAdapter =新DemoFragmentAdapter(getSupportFragmentManager());

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

        UnderlinePageIndicator指标=(UnderlinePageIndicator)findViewById(R.id.indicator);
        indicator.setViewPager(mPager);
        indicator.setFades(假);
        mIndicator =指标;
    }

}
 

DemoFragmentAdapter.java

 类DemoFragmentAdapter扩展FragmentPagerAdapter {
    受保护的静态最终诠释[] CONTENT =新INT [] {R.drawable.demo1,R.drawable.demo2,R.drawable.demo3,R.drawable.demo4};

    私人诠释mCount = CONTENT.length;

    公共DemoFragmentAdapter(FragmentManager FM){
        超(FM);
    }

    @覆盖
    公共片段的getItem(INT位置){
        返回DemoFragment.newInstance(含量[位置%CONTENT.length]);
    }

    @覆盖
    公众诠释getCount将(){
        返回mCount;
    }

    公共无效setCount(诠释计数){
        如果(计数大于0和放大器;&安培; COUNT< = 10){
            mCount =计数;
            notifyDataSetChanged();
        }
    }}
 

DemoFragment.java

 公开最后一类DemoFragment扩展片段{
    私有静态最后弦乐KEY_CONTENT =TestFragment:内容;

    公共静态DemoFragment的newInstance(INT内容){
        DemoFragment片段=新DemoFragment();
        fragment.mContent =内容;
        返回片段;
    }

    私人诠释mContent;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        如果((savedInstanceState = NULL)及!&安培; savedInstanceState.containsKey(KEY_CONTENT)){
            mContent = savedInstanceState.getInt(KE​​Y_CONTENT);
        }
    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){

        ImageView的形象=新ImageView的(getActivity());
        image.setBackgroundResource(mContent);

        的LinearLayout布局=新的LinearLayout(getActivity());
        layout.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        layout.setGravity(Gravity.CENTER);
        layout.addView(图像);

        返回布局;
    }

    @覆盖
    公共无效的onSaveInstanceState(包outState){
        super.onSaveInstanceState(outState);
        outState.putInt(KE​​Y_CONTENT,mContent);
    }
}
 

解决方案

把你的演示的信息在不同的活动,并给它下面的主题。

 <样式名称=透明父=@安卓风格/ Theme.NoTitleBar>
    <项目名称=机器人:windowContentOverlay> @空< /项目>
    <项目名称=机器人:windowIsTranslucent>真< /项目>
    <项目名称=机器人:windowBackground> @android:彩色/透明< /项目>
    <项目名称=机器人:windowNoTitle>真< /项目>
    <项目名称=机器人:backgroundDimEnabled>假< /项目>
< /风格>
 

如果您使用的是ActionBarSherlock变化 @风格/ Theme.Sherlock

这会给你一个透明的活动,这样你就可以看到它下面的活动。

现在我猜你想有一个半透明的背景了。

在XML布局(你透明的活动)地址:

 安卓背景=#aa000000
 

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

第2定义的不透明度:00是100%透明,FF是100%不透明。所以选择介于两者之间。

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:

Galaxy Nexus

Nexus One

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 ViewPagerIndicator library. 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'.

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?

Thanks!

Here's my code for the first attempt:

DemoActivity.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

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

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);
    }
}

解决方案

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 parent to @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:

android:background="#aa000000" 

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

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

 
精彩推荐