啟動頁(Splash)常見需求:顯示品牌 Logo、初始化資料、延遲幾秒進入主介面。兩種主流實作方式各有優缺點。
<activity android:name=".SplashActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
public class SplashActivity extends Activity {
private static final int SPLASH_DURATION = 3000; // 3 秒
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash); // 包含 Logo ImageView 等
new Handler().postDelayed(() -> {
startActivity(new Intent(this, MainActivity.class));
finish(); // 關閉 Splash,避免 Back 返回
}, SPLASH_DURATION);
}
}
優點:邏輯清晰、生命週期分離。缺點:啟動兩個 Activity 稍增開銷,需在 Manifest 註冊兩個入口。
<FrameLayout xmlns:android="..." android:layout_width="match_parent" android:layout_height="match_parent">
<!-- 主內容 -->
<WebView android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent" />
<!-- Splash 覆蓋層 -->
<LinearLayout android:id="@+id/splash_overlay"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:gravity="center"
android:background="@color/white">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/splash_logo" android:layout_marginBottom="16dp"/>
<TextView android:id="@+id/splash_text" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="正在初始化..." android:textColor="@color/black"/>
</LinearLayout>
</FrameLayout>
public class MainActivity extends Activity {
private static final long SPLASH_DURATION = 1000;
private LinearLayout splashOverlay;
private TextView splashText;
private Animation fadeIn, fadeOut;
private final Handler splashHandler = new Handler(msg -> {
if (msg.what == 0) {
splashOverlay.startAnimation(fadeOut);
splashOverlay.setVisibility(View.GONE);
}
return true;
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
splashOverlay = findViewById(R.id.splash_overlay);
splashText = findViewById(R.id.splash_text);
fadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in); // alpha 0→1
fadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out); // alpha 1→0
splashOverlay.startAnimation(fadeIn);
splashHandler.sendEmptyMessageDelayed(0, SPLASH_DURATION);
// 這裡可並行進行資料初始化、網路請求等
}
}
<alpha xmlns:android="..." android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500"/>
<alpha xmlns:android="..." android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500"/>
| 維度 | 雙 Activity | 單 Activity View 切換 |
|---|---|---|
| 啟動速度 | 稍慢(兩次 onCreate) | 快(單次 onCreate) |
| 動畫流暢度 | 可能有白閃 | 無縫銜接,可做淡入淡出 |
| 初始化並行度 | Splash 期間可做初始化 | 同樣可在 Handler 前啟動異步任務 |
| 程式碼複雜度 | 低 | 中(需管理 View 狀態) |
| 適用場景 | 簡單專案、舊版本相容 | 追求體驗、現代應用首選 |
現代最佳實踐 (Android 12+): 使用 SplashScreen API (
androidx.core:splashscreen),在onCreate前由系統自動顯示啟動圖示,支援動畫、圖示自適應、無需自定義 Activity,詳見官方文件 SplashScreen Compat Library。