[英]How to implement splash screen in android
我有一個顯示項目列表的應用程序。 我需要在主要活動開始前顯示淡出圖像。 我在onCreate主要活動方法中嘗試了類似的東西,我開始了一個動畫活動
Intent animationIntent=new Intent(this,AnimationActivity.class);
startActivityForResult(AnimationIntent);
在AnimationActivity.class的onCreate方法中
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView introanim = (ImageView) findViewById(R.id.imgView);
AnimationSet StoryAnimation = (AnimationSet)AnimationUtils.
loadAnimation(this, R.anim.alphanim);
StoryAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
AnimationSubActivity.this.finish();
}
@Override
public void onAnimationEnd(Animation animation) {
AnimationSubActivity.this.finish();
}
});
introanim.clearAnimation();
introanim.startAnimation(StoryAnimation);
我的main.xml是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/imgView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/icon"
/>
</LinearLayout>
這種方法的問題在於它顯示了一個動畫,但是imageview來了一段時間,主要活動帶有幻燈片過渡效果
但我希望我的形象淡出,我的活動應該淡入
任何幫助將不勝感激。 提前致謝
基本上,你正在尋找的是一個Splash屏幕,它會顯示你的圖像,然后淡出。 然后主要活動的屏幕淡入。所以你可以做的是為Splash屏幕創建一個活動,然后為你可能想要調用的主要活動創建另一個活動。 這將是您的Splash Screen活動。
public class SplashScreen extends Activity {
private static final int SPLASH_DISPLAY_TIME = 4000; // splash screen delay time
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent();
intent.setClass(Splash.this, NextActivity.class);
Splash.this.startActivity(intent);
Splash.this.finish();
// transition from splash to main menu
overridePendingTransition(R.animate.activityfadein,
R.animate.splashfadeout);
}
}, SPLASH_DISPLAY_TIME);
}
NextActivity是您希望淡入並取而代之的任何活動。 對於動畫,您需要在資源中名為animate的文件夾中創建兩個xml文件。 這是splashfadeout.xml文件
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:zAdjustment="top" android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="4000" />
這將是activityfadein.xml文件
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" />
這些文件基本上使您的活動淡入淡出
像@Manish Burman所暗示的“Splash Screen”並不是真實的。 它實際上只是一個“全屏商業”,沒有用戶願意花時間看。 除了存在之外它沒有任何意義,這意味着它不會掩蓋該區域中的長加載時間或某些東西,就像真正的閃屏一樣。
如果你要顯示一個啟動畫面,那應該是因為你有一個需要花費很長時間才能加載的初始活動 - 然后可以證明啟動畫面是合理的,向用戶顯示正在加載的東西或說服他該應用程序已經沒有“死”。
查看本指南,了解如何創建真正的啟動畫面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.