问题描述
我正在尝试创建一个仅在用户首次安装我的应用程序时启动的半透明演示屏幕.以下是 Pulse News 应用中的示例:
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.
对于我的第一次尝试,我修改了 ViewPagerIndicator 库中的一个示例.我在每个视图寻呼机片段内的 ImageViews 中使用了半透明 PNG.然后,我在主要活动"的 onCreate 方法中将此作为演示活动"启动.
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?
我还有另一个相关问题,这取决于这是如何实现的.我正在尝试覆盖文本和箭头,以便它们指向背景中的特定 UI 组件.通过使用具有文本和箭头的 PNG,它可能无法在不同设备上正确缩放.即,箭头可能不一定指向后台正确的 UI 组件.有没有办法解决这个问题?
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?
谢谢!
这是我第一次尝试的代码:
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>
如果您使用 ActionBarSherlock,请将 parent
更改为 @style/Theme.Sherlock
.
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.
在 xml 布局(您的透明活动)中添加:
In the xml layout (of your transparent activity) add:
android:background="#aa000000"
最后 6 位数字定义颜色:000000 为黑色.
The last 6 digits define the color: 000000 is black.
前 2 个定义不透明度:00 是 100% 透明,ff 是 100% 不透明.因此,请选择介于两者之间的内容.
The first 2 define the opacity: 00 is 100% transparent, ff is 100% opaque. So choose something in between.
这篇关于如何为 Android 应用创建透明的演示屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!