android splash screen (android 启动屏幕)
访问量: 2152
refer to: http://stackoverflow.com/questions/5486789/how-do-i-make-a-splash-screen
1. 添加splash_activity:
package topgroup.com.topgroupandroid; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import topgroup.com.topgroupandroid.R; public class SplashActivity extends Activity { /** Duration of wait **/ private final int SPLASH_DISPLAY_LENGTH = 1000; /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.splash_activity); /* New Handler to start the Menu-Activity * and close this Splash-Screen after some seconds.*/ new Handler().postDelayed(new Runnable(){ @Override public void run() { /* Create an Intent that will start the Menu-Activity. */ Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class); SplashActivity.this.startActivity(mainIntent); SplashActivity.this.finish(); } }, SPLASH_DISPLAY_LENGTH); } }
2. 添加 splash.png 到 res/drawable 中
3. 添加 res/layout/splash_activity.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/splash_screen" android:layout_width="wrap_content" android:layout_height="fill_parent" android:src="@drawable/splash" android:layout_gravity="center" /> </LinearLayout>
4. 修改 AndroidManifest.xml , 让app的入口activity 是 splash activity:
<activity
android:name=".SplashActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <activity
android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
</activity>