萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> Android 開發

Android 啟動閃黑屏徹底解決:Theme、WindowBackground 與啟動優化實戰

問題根因

App 啟動時,系統先建立進程、載入類別、執行 Application.onCreate()、啟動主 Activity、執行 onCreate()、onResume()、佈局膨脹與繪製,這段期間畫面是空白的,預設顯示為黑色(或白色,視主題而定),即所謂「閃黑屏」或「啟動白屏」。

方案一:自訂 Theme 設定 windowBackground(推薦)

利用系統在 Activity 啟動前即根據 Theme 繪製 windowBackground 的特性,預先顯示品牌圖、啟動圖或品牌色,覆蓋預設黑/白背景。

res/values/styles.xml

<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>

AndroidManifest.xml 套用 Theme

<application ...>
    <activity
        android:name=".MainActivity"
        android:theme="@style/Theme.AppStartLoad"
        android:exported="true">
        <intent-filter>...</intent-filter>
    </activity>
</application>

兩種方案優缺點對比

方案優點缺點
windowBackground 圖片 啟動極快有畫面、品牌曝光、無感知延遲 若初始化耗時長,畫面會靜止在啟動圖,感覺「卡住」;需確保圖片與正式 UI 視覺銜接自然
windowIsTranslucent UI 一次性完整呈現、無殘留畫面切換感 體感啟動較慢(實際耗時相同),透明期間若上一畫面是深色會顯得閃爍

進階:啟動畫面與主畫面無縫銜接 (Splash Screen API)

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 的標準做法。

其他造成「閃屏」的場景與對策

  • Activity 切換間隙:目標 Activity 同樣套用上述 Theme,或使用 ActivityOptions.makeSceneTransitionAnimation() 共享元素過渡。
  • 初始化耗時過長:將非必要初始化移至背景執行緒 (Coroutine/RxJava/WorkManager)、採用 ApplicationStartUp (Jetpack Startup) 延遲初始化、啟用 Baseline Profiles 優化類別載入。
  • 大圖載入:啟動畫面圖片使用 layer-list 或 VectorDrawable,避免解碼大尺寸 Bitmap。

Theme vs Style 概念釐清

特性StyleTheme
作用範圍單一 View (Button, TextView...)整個 Application 或單一 Activity
套用方式style="@style/Widget.Button.Primary"Manifest android:theme="@style/Theme.App"
屬性衝突優先序Style > Theme > 默認值—
常見屬性textSize, textColor, background, paddingwindowBackground, colorPrimary, windowNoTitle, windowIsTranslucent

常用系統 Theme 速查

@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,才能從根本上縮短體感啟動時間。

copyright © 萬盛學電腦網 all rights reserved