我有情况,我希望在3秒后改变活动的布局。何我能做到这一点? 例如应用程序开始启动画面,它会自动运行3秒钟,然后切换布局应用程序的第一个屏幕布局。这应该在相同的活动发生,任何想法?
I have situation where I want to change layout of activity after 3 seconds. Ho can I do that? For example application starts with splashscreen that will run for 3 seconds then automatically switch layout to application's first screen layout. This should happen in the same activity, any ideas?
感谢
我这样做只使用一个XML布局。我只是把一个额外的RelativeLayout的在它的重新presents我介绍屏幕,然后我用一个淡出动画就可以了,然后调用.setVisibility(View.GONE)。
I did this using only one xml layout. I just put an extra RelativeLayout in it that represents my intro screen then I use a fadeOut Animation on it and then call .setVisibility(View.GONE).
这是我的main.xml布局文件的一部分
This is part of my main.xml layout file
<RelativeLayout
android:id="@+id/introLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
>
<ImageView
android:id="@+id/logoImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/logo">
</ImageView>
</RelativeLayout>
然后在我的活动我有这样的:
Then inside my activity I have this:
introLayout = (RelativeLayout) findViewById(R.id.introLayout);
Animation fadeOutAnim = AnimationUtils.loadAnimation(MyActivity.this, R.anim.fadeout);
introLayout.startAnimation(fadeOutAnim);
introLayout.setVisibility(View.GONE);
您可以通过将startAnimation(),以及所述setVisibility的一个可运行的内部和使用postDelayed()如上述马库斯使3秒后该运行。不要考虑做一些工作,而这个介绍布局在屏幕上的,所以它不只是有3秒钟的延迟,为用户。也许检查以查看是否该应用程序的运行版本是当前版本或没有。
You could make this run after 3 seconds by putting the startAnimation(), and the setVisibility inside of a runnable and using postDelayed() as markus mentioned. Do consider doing some work while this intro layout is on the screen though, so its not just a 3 second delay for the user. Perhaps check to see if the running version of the application is the current version or not.
编辑:
你需要的fadout.xml文件添加到 / RES /动画/
(创建动画目录,如果它不存在)。下面是一个例子。
You'll need to add the fadout.xml file to /res/anim/
(create the anim directory if it doesn't exist). Here is an example.
fadeout.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="700"
android:fillAfter="true"/>