App 啟動時,系統先建立進程、載入類別、執行 Application.onCreate()、啟動主 Activity、執行 onCreate()、onResume()、佈局膨脹與繪製,這段期間畫面是空白的,預設顯示為黑色(或白色,視主題而定),即所謂「閃黑屏」或「啟動白屏」。
利用系統在 Activity 啟動前即根據 Theme 繪製 windowBackground 的特性,預先顯示品牌圖、啟動圖或品牌色,覆蓋預設黑/白背景。
<resources>
<!-- 方案 A:顯示品牌啟動圖 -->
<style name="Theme.AppStartLoad" parent="android:Theme">
<item name="android:windowBackground">@drawable/launch_bg</item>
<item name="android:windowNoTitle">true</item>
</style>
<!-- 方案 B:透明背景,Activity 畫面一次性渲染完成再顯示 -->
<style name="Theme.AppStartLoadTranslucent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
@drawable/launch_bg 建議使用 layer-list 組合背景色 + 居中 Logo,避免單張大圖造成 OOM:
<!-- res/drawable/launch_bg.xml -->
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@color/brand_primary" />
<item>
<bitmap android:src="@mipmap/ic_launcher_foreground"
android:gravity="center" />
</item>
</layer-list>
<application ...>
<activity
android:name=".MainActivity"
android:theme="@style/Theme.AppStartLoad"
android:exported="true">
<intent-filter>...</intent-filter>
</activity>
</application>
| 方案 | 優點 | 缺點 |
|---|---|---|
| windowBackground 圖片 | 啟動極快有畫面、品牌曝光、無感知延遲 | 若初始化耗時長,畫面會靜止在啟動圖,感覺「卡住」;需確保圖片與正式 UI 視覺銜接自然 |
| windowIsTranslucent | UI 一次性完整呈現、無殘留畫面切換感 | 體感啟動較慢(實際耗時相同),透明期間若上一畫面是深色會顯得閃爍 |
Android 12 (API 31) 引入 SplashScreen API,標準化啟動畫面行為:
<!-- res/values/themes.xml -->
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/brand_primary</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_splash_animated</item>
<item name="postSplashScreenTheme">@style/Theme.App</item>
</style>
// MainActivity.onCreate() 最前面,在 super.onCreate() 之前
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
getSplashScreen().setOnExitAnimationListener(splashScreenView -> {
// 可選:自訂退出動畫
splashScreenView.remove();
});
}
此 API 自動處理動畫、過渡、深色模式適配,是現代 App 的標準做法。
ActivityOptions.makeSceneTransitionAnimation() 共享元素過渡。ApplicationStartUp (Jetpack Startup) 延遲初始化、啟用 Baseline Profiles 優化類別載入。layer-list 或 VectorDrawable,避免解碼大尺寸 Bitmap。| 特性 | Style | Theme |
|---|---|---|
| 作用範圍 | 單一 View (Button, TextView...) | 整個 Application 或單一 Activity |
| 套用方式 | style="@style/Widget.Button.Primary" | Manifest android:theme="@style/Theme.App" |
| 屬性衝突優先序 | Style > Theme > 默認值 | — |
| 常見屬性 | textSize, textColor, background, padding | windowBackground, colorPrimary, windowNoTitle, windowIsTranslucent |
@android:style/Theme.Dialog // 對話框樣式
@android:style/Theme.NoTitleBar // 無標題列
@android:style/Theme.NoTitleBar.Fullscreen // 全螢幕無標題
@android:style/Theme.Translucent // 透明背景
@android:style/Theme.Translucent.NoTitleBar.Fullscreen // 透明全螢幕
Theme.MaterialComponents.DayNight // Material Design 3 基礎主題 (推薦)
解決啟動閃黑屏的核心是「讓使用者在第一幀就看到有意義的畫面」。windowBackground + layer-list 是相容性最好、實作最簡單的方案;Android 12+ 專案請直接遷移至 SplashScreen API。並行優化初始化流程、啟用 Baseline Profiles,才能從根本上縮短體感啟動時間。