啟動畫面

自 Android 12 起, SplashScreen API 可讓應用程式啟動 含動畫,包括啟動時的應用程式內動態、顯示啟動畫面 和應用程式本身進行轉換。SplashScreen 是一種 Window和 因此涵蓋了 Activity

圖 1.啟動畫面。

啟動畫面體驗為所有應用程式提供標準設計元素 而且還能自訂發布方式,讓應用程式保有獨特的品牌宣傳元素。

除了使用 SplashScreen 平台 API 之外,您也可以使用 SplashScreen Compat 程式庫,其中納入 SplashScreen API。

啟動畫面的運作方式

使用者在應用程式程序未執行的情況下啟動應用程式 (冷 start) 或 Activity 不是 建立 (暖啟動)、 會發生下列情況:

  1. 系統會使用主題和您自訂的任何動畫顯示啟動畫面 定義。

  2. 應用程式準備就緒後,系統就會關閉啟動畫面並顯示應用程式。

範例開始

啟動畫面的元素和機制

啟動畫面的元素是由 Android 資訊清單檔案。每個元素都有淺色和深色模式版本。

啟動畫面的可自訂元素由應用程式圖示、圖示組成 背景和視窗背景:

顯示啟動畫面所含元素的圖片
圖 2. 可自訂的啟動畫面元素 。

請參考圖 2 所示的下列元素:

1 應用程式圖示必須是向量可繪項目。這項服務 可以是靜態或動畫雖然動畫沒有播放時間限制 建議您不要超過 1,000 毫秒預設圖示為啟動器圖示。

2 圖示背景是選用項目,如果可以 增加圖示和視窗背景之間的對比度。如果使用 自動調整圖示、 當與視窗背景的對比度夠高時,就會顯示此畫面。

3 如同自動調整圖示, 前景就會被遮蓋。

4 視窗背景包含單一 不透明顏色。如果視窗背景已設定且為純色,就會使用此背景 如未設定該屬性,就會使用預設值。

啟動畫面尺寸

啟動畫面圖示使用的規格與 自動調整圖示、 如下所示:

  • 品牌圖片:尺寸必須為 200×80 dp。
  • 包含圖示背景的應用程式圖示:尺寸必須為 240×240 dp,並符合 圓形 160 dp (直徑為 160 dp)。
  • 不含圖示背景的應用程式圖示:尺寸必須為 288 × 288 dp,且符合 一個圓圈為直徑 192 dp

舉例來說,如果圖片的原尺寸為 300×300 dp,圖示應符合直徑 200 dp 的圓環。圓形外的一切都會轉動 隱藏 (遮蓋)。

這張圖片顯示實心和透明背景的各種圖示尺寸
圖 3. 啟動畫面圖示尺寸 請使用實心和透明背景。

啟動畫面動畫和啟動順序

額外延遲時間通常與在冷啟動時啟動應用程式有關。 在啟動畫面中加上動畫圖示,不僅美觀, 並提供更優異的使用體驗使用者研究顯示,新創公司認為 瀏覽動畫的時間較短

啟動畫面動畫內嵌在啟動序列元件中, 如圖 4 所示

以 12 個連續頁框顯示啟動順序的圖片。從輕觸啟動器圖示開始,並在放大時填滿畫面
圖 4. 啟動序列。
  1. 輸入動畫:加入系統檢視畫面到啟動畫面。這項服務 由系統控制,無法自訂。

  2. 啟動畫面 (在序列的「等待」部分顯示):啟動畫面 還可以自訂螢幕、提供自己的標誌動畫 品牌必須符合規定 本網頁中所述,才能正常運作。

  3. 離開動畫:這包括會隱藏啟動畫面的動畫。 如要自訂,請使用 SplashScreenView 及其 圖示。您可以利用轉場效果 (包括轉換設定、 透明度和顏色在此情況下,請在出現「錯誤」指令時,手動移除啟動畫面 都沒有問題

執行圖示動畫時,應用程式啟動可讓您選擇略過 序列。應用程式會觸發 onResume() 否則啟動畫面會自動逾時 因此能輕鬆略過啟動畫面只能使用 onResume() 關閉 應用程式從視覺的角度穩定時,因此不會有額外的旋轉圖示 。推出不完整的介面可能會讓使用者感到困擾, 營造無法預測或欠缺效果的印象。

啟動畫面動畫要求

啟動畫面必須符合以下規格:

  • 設定單一視窗不透明的背景顏色。白天黑夜 支援模式 SplashScreen Compat 程式庫

  • 請確認動畫圖示符合下列規格:

    • 格式:圖示必須是 AnimatedVectorDrawable (AVD) XML
    • 尺寸:AVD 圖示必須設為自動調整大小的四倍 圖示,如下所示:
      • 圖示區域必須是 432 dp,也就是 未遮蓋的自動調整圖示區域的 108 dp 區域。
      • 圖片的內側三分之二會顯示在啟動器圖示上 須為 288 dp,也就是 72 dp 的 4 倍 組成自動調整圖示的內遮罩區域。
    • 時間長度:建議手機不要超過 1,000 毫秒。別擔心!您可以使用 延遲時間但不得超過 166 毫秒。如果應用程式 啟動時間超過 1,000 毫秒,請考慮使用循環播放動畫。
  • 設定適當的關閉時間,以關閉啟動畫面, 應用程式就會繪製第一個影格您可以按照 在「探索」部分 延長啟動畫面在螢幕上的停留時間

啟動畫面資源

圖 5.AVD 範例。

下載 入門套件範例 ,示範如何建立動畫、設定動畫格式,以及如何將動畫匯出至 AVD。 包含的內容如下:

  • 動畫的 Adobe After Effects 專案檔案。
  • 最終匯出的 AVD XML 檔案。
  • 動畫的 GIF 範例。

下載這些檔案即表示您同意接受 Google 服務條款

Google 隱私權政策》說明瞭 所有資料都會在這項服務中處理。

自訂應用程式中的啟動畫面

根據預設,如果符合以下條件,SplashScreen 會使用主題的 windowBackground windowBackground 是單一顏色。如要自訂啟動畫面,請新增 對應至應用程式佈景主題

您可以執行下列任一操作,自訂應用程式的啟動畫面:

  • 設定主題屬性來變更外觀。

  • 延長在螢幕上的停留時間。

  • 自訂用於關閉啟動畫面的動畫。

開始使用

核心 SplashScreen 程式庫可將 Android 12 啟動畫面提供給所有人 搭載 API 23 的裝置。如要將實驗工具加到專案,請將下列程式碼片段新增至 您的 build.gradle 檔案:

Groovy

dependencies {
    implementation "androidx.core:core-splashscreen:1.0.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-splashscreen:1.0.0")
}

為啟動畫面設定一個主題以變更外觀

您可以在 Activity 主題中指定下列屬性進行自訂 應用程式的啟動畫面如果已有舊版啟動畫面 使用 android:windowBackground 等屬性的導入方式 並為 Android 12 以上版本提供替代資源檔案

  1. 使用 windowSplashScreenBackground敬上 將背景填滿特定單一顏色:

    <item name="android:windowSplashScreenBackground">@color/...</item>
    
  2. 使用 windowSplashScreenAnimatedIcon敬上 取代起始視窗中央的圖示。

    如果應用程式僅指定 Android 12 (API 級別 32) 為目標,請按照下列步驟操作:

    如果該物件為虛構物件,且可繪項目 AnimationDrawable敬上 和AnimatedVectorDrawable,將 windowSplashScreenAnimationDuration 設為 在顯示起始視窗時播放動畫。這不是必要步驟 對於 Android 13 系統來說,這個時間長度是由 AnimatedVectorDrawable

    <item name="android:windowSplashScreenAnimatedIcon">@drawable/...</item>
    
  3. 使用 windowSplashScreenAnimationDuration敬上 表示啟動畫面圖示動畫的持續時間。設定這項 不會影響啟動畫面的實際顯示時間 但您可以在自訂啟動畫面結束事件時擷取此方法。 動畫 SplashScreenView.getIconAnimationDuration。 請參閱下一節,瞭解 延長啟動畫面在螢幕上的停留時間 ,掌握更多詳細資訊。

    <item name="android:windowSplashScreenAnimationDuration">1000</item>
    
  4. 使用 windowSplashScreenIconBackgroundColor 設定啟動畫面圖示後方的背景。這非常實用 視窗背景與圖示之間的對比不足。

    <item name="android:windowSplashScreenIconBackgroundColor">@color/...</item>
    
  5. 別擔心!您可以使用 windowSplashScreenBrandingImage敬上 來設定要顯示在啟動畫面底部的圖片。不過, 設計指南則不建議使用品牌宣傳圖片。

    <item name="android:windowSplashScreenBrandingImage">@drawable/...</item>
    
  6. 別擔心!您可以使用 windowSplashScreenBehavior敬上 指定應用程式是否要一律在啟動畫面顯示圖示 Android 13 以上版本。預設值為 0,表示會在 如果啟動活動將 splashScreenStyle 設為 SPLASH_SCREEN_STYLE_ICON, 或者,如果啟動活動並未指定 。如果您不想顯示空白的啟動畫面,而且希望 的動畫圖示,請將這個值設為 icon_preferred

    <item name="android:windowSplashScreenBehavior">icon_preferred</item>
    

延長啟動畫面在螢幕上的停留時間

應用程式會在首次繪製頁框時立即關閉啟動畫面。如果發生以下情況: 您必須載入少量資料,例如從電腦快速載入應用程式內設定 非同步本機磁碟 ViewTreeObserver.OnPreDrawListener敬上 暫停應用程式,開始繪製第一個影格。

如果開始活動是在繪圖前完成 (例如,您並未在繪圖前完成) 設定內容檢視畫面,並在 onResume 前結束 (預先繪製) 即可。

Kotlin

// Create a new event for the activity.
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // Set the layout for the content view.
    setContentView(R.layout.main_activity)

    // Set up an OnPreDrawListener to the root view.
    val content: View = findViewById(android.R.id.content)
    content.viewTreeObserver.addOnPreDrawListener(
        object : ViewTreeObserver.OnPreDrawListener {
            override fun onPreDraw(): Boolean {
                // Check whether the initial data is ready.
                return if (viewModel.isReady) {
                    // The content is ready. Start drawing.
                    content.viewTreeObserver.removeOnPreDrawListener(this)
                    true
                } else {
                    // The content isn't ready. Suspend.
                    false
                }
            }
        }
    )
}

Java

// Create a new event for the activity.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set the layout for the content view.
    setContentView(R.layout.main_activity);

    // Set up an OnPreDrawListener to the root view.
    final View content = findViewById(android.R.id.content);
    content.getViewTreeObserver().addOnPreDrawListener(
            new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    // Check whether the initial data is ready.
                    if (mViewModel.isReady()) {
                        // The content is ready. Start drawing.
                        content.getViewTreeObserver().removeOnPreDrawListener(this);
                        return true;
                    } else {
                        // The content isn't ready. Suspend.
                        return false;
                    }
                }
            });
}

自訂用於關閉啟動畫面的動畫

您可以透過 Activity.getSplashScreen() 進一步自訂啟動畫面的動畫。

Kotlin

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // ...

    // Add a callback that's called when the splash screen is animating to the
    // app content.
    splashScreen.setOnExitAnimationListener { splashScreenView ->
        // Create your custom animation.
        val slideUp = ObjectAnimator.ofFloat(
            splashScreenView,
            View.TRANSLATION_Y,
            0f,
            -splashScreenView.height.toFloat()
        )
        slideUp.interpolator = AnticipateInterpolator()
        slideUp.duration = 200L

        // Call SplashScreenView.remove at the end of your custom animation.
        slideUp.doOnEnd { splashScreenView.remove() }

        // Run your animation.
        slideUp.start()
    }
}

Java

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ...

    // Add a callback that's called when the splash screen is animating to the
    // app content.
    getSplashScreen().setOnExitAnimationListener(splashScreenView -> {
        final ObjectAnimator slideUp = ObjectAnimator.ofFloat(
                splashScreenView,
                View.TRANSLATION_Y,
                0f,
                -splashScreenView.getHeight()
        );
        slideUp.setInterpolator(new AnticipateInterpolator());
        slideUp.setDuration(200L);

        // Call SplashScreenView.remove at the end of your custom animation.
        slideUp.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                splashScreenView.remove();
            }
        });

        // Run your animation.
        slideUp.start();
    });
}

開始此回呼時 動畫向量可繪項目 啟動畫面時顯示。根據應用程式啟動的時間長度, 可繪項目可能會顯示在動畫的中間。使用 SplashScreenView.getIconAnimationStart敬上 顯示動畫何時開始可按照以下方法計算圖示動畫的剩餘時間:

Kotlin

// Get the duration of the animated vector drawable.
val animationDuration = splashScreenView.iconAnimationDuration
// Get the start time of the animation.
val animationStart = splashScreenView.iconAnimationStart
// Calculate the remaining duration of the animation.
val remainingDuration = if (animationDuration != null && animationStart != null) {
    (animationDuration - Duration.between(animationStart, Instant.now()))
        .toMillis()
        .coerceAtLeast(0L)
} else {
    0L
}

Java

// Get the duration of the animated vector drawable.
Duration animationDuration = splashScreenView.getIconAnimationDuration();
// Get the start time of the animation.
Instant animationStart = splashScreenView.getIconAnimationStart();
// Calculate the remaining duration of the animation.
long remainingDuration;
if (animationDuration != null && animationStart != null) {
    remainingDuration = animationDuration.minus(
            Duration.between(animationStart, Instant.now())
    ).toMillis();
    remainingDuration = Math.max(remainingDuration, 0L);
} else {
    remainingDuration = 0L;
}

其他資源