自 Android 12 起,在搭載 Android 12 以上版本的裝置上執行應用程式時,SplashScreen
API 會為所有應用程式啟用新的應用程式啟動動畫。這包括啟動時的應用程式內建動態、顯示應用程式圖示的啟動畫面,以及應用程式自身的轉換。

新版服務每次推出應用程式時,都會提供標準設計元素,但您也可以自訂應用程式,讓應用程式保有獨特的品牌宣傳元素。
除了使用 SplashScreen
平台 API 之外,您也可以使用 SplashScreen
Compat 程式庫來包裝 SplashScreen
API。
啟動畫面的運作方式
當使用者在應用程式執行程序未執行時啟動應用程式(冷啟動)或活動尚未建立(暖啟動)時,會發生下列事件。(熱啟動期間一律不會顯示啟動畫面)。
系統會根據你定義的主題和任何動畫來顯示啟動畫面。
應用程式就緒後,即可關閉啟動畫面並顯示應用程式。
啟動畫面的元素和機制
啟動畫面的元素由 Android 資訊清單中的 XML 資源檔案定義。每種元素都有淺色和深色模式版本。
啟動畫面的可自訂元素包含應用程式圖示、圖示背景和視窗背景:

請留意這些元素的以下注意事項:
1 應用程式圖示應該是一個向量可繪項目,可以是靜態或者是動畫。雖然動畫具有無限長度,我們還是建議不要超過 1,000 毫秒。根據預設,使用啟動器圖示。
2 圖示背景是選用項目,當您需要比較圖示和視窗背景之間的對比時,這項功能非常實用。如果使用自動調整圖示,當視窗背景的對比度夠高時,就會顯示其背景。3 如同自動調整圖示,有三分之一的前景會被遮蓋。
4 視窗背景包含單一不透明顏色。如果已設定視窗背景且為純色,當未設定屬性時,則根據預設使用此背景。
啟動畫面尺寸
啟動畫面圖示使用的規格與自動調整圖示相同,如下所示:
- 品牌圖片:大小應為 200×80 dp。
- 含有圖示背景的應用程式圖示:大小應為 240×240 dp,且符合直徑為 160 dp 的圓環。
- 不含圖示背景的應用程式圖示:大小應為 288×288 dp,且符合直徑為 192 dp 的圓環。
舉例來說,如果圖片的原尺寸為 300×300 dp,圖示應符合直徑 200 dp 的圓環。圓環以外的所有內容都會被隱藏(遮蓋)。

啟動畫面動畫和啟動順序
其他延遲通常與在冷啟動時啟動應用程式有關。在啟動畫面中添加動畫圖示,不僅美觀吸引人,而且提供更優質的使用體驗,而且還有一項好處:使用者研究顯示,查看動畫時,感知啟動時間較少。
啟動畫面動畫內嵌於下列啟動順序元件中。
輸入動畫:加入系統檢視至啟動畫面。它由系統控制,無法自訂。
啟動顯示畫面:您可以自訂啟動畫面,以提供自己的標誌動畫和品牌宣傳內容。必須符合本文所述的需求才能正常運作。
離開動畫:這包括會隱藏啟動畫面的動畫執行作業。如需自訂,必須有
SplashScreenView
及其圖示的存取權限,並可在其中放送任意動畫、設定其變形、不透明度和顏色。如果發生這種情況,必須在動畫結束後手動移除啟動畫面。
執行圖示動畫時,應用程式啟動可讓您選擇在應用程式提早準備就緒時略過序列。應用程式會自動觸發 onResume()
或啟動螢幕自動逾時,因此請確保動作能夠順暢地略過。當啟動作業從視覺的角度穩定時,啟動螢幕才應使用 onResume()
關閉,因此不需要額外的旋轉圖示。介紹不完整的介面可能會讓使用者感到困擾,而且可能會讓使用者產生無法預測或缺乏預期的印象。
啟動畫面動畫要求
啟動畫面應符合下列規格:
設定單一視窗不透明背景色。「日間」和「夜間」模式皆支援Splash Screen Compat 程式庫。
請確認動畫圖示符合下列規格:
- 格式:這必須是動畫向量可繪項目 (AVD) XML。
- 尺寸:AVD 圖示必須是自動調整圖示大小的四倍,如下所示:
- 圖示區域必須為 432 dp (也就是未遮蓋的自動調整圖示面積 108 dp 的 4 倍)。
- 圖片的前三分之二會顯示在啟動器圖示上,且大小必須為 288 dp (也就是代表自動調整圖示內部遮罩區域 72 dp 的 4 倍)。
- 時間長度:建議您不要在手機上超過 1,000 毫秒。您可以使用延遲啟動,但長度不得超過 166 毫秒。如果應用程式啟動時間超過 1,000 毫秒,請考慮使用循環播放動畫。
設定適當的關閉時間,以關閉啟動畫面 (當應用程式繪製第一個影格時)。您可以按照「保持啟動畫面的停留時間較長」一文的說明,進一步自訂這項設定。
啟動畫面資源

請下載我們的入門套件範例,將會示範如何將動畫建立、格式化以及匯出成 AVD。
- 動畫的 Adobe After Effects 專案檔案
- 最終匯出的 AVD XML 檔案
- 動畫 GIF 範例
下載這些檔案即表示您同意《Google 服務條款》。
有關這項服務處理資料的方式,可以在《Google 隱私權政策》找到說明。
自訂應用程式的啟動畫面
根據預設,如果是單色及啟動器圖示,則 SplashScreen
使用 windowBackground
主題。在應用程式主題中新增屬性,才能自訂啟動畫面。
可以透過下列任一方式自訂應用程式的啟動畫面:
設定主題屬性以變更外觀
延長在螢幕上的停留時間
自訂用於關閉啟動畫面的動畫
為啟動畫面設定一個主題以變更外觀
你可以在「活動」主題中指定下列屬性,藉此自訂應用程式的啟動畫面。如果您繼續使用舊版啟動畫面 (使用 android:windowBackground
等屬性),請考慮提供 Android 12 及以上版本的替代資源檔案。
使用
windowSplashScreenBackground
在背景中填入特定單一顏色:<item name="android:windowSplashScreenBackground">@color/...</item>
使用
windowSplashScreenAnimatedIcon
取代起始視窗中央的圖示。如果物件透過AnimationDrawable
和AnimatedVectorDrawable
建立動畫屬性及可繪項目,則也必須設定windowSplashScreenAnimationDuration
以在顯示起始視窗的同時播放動畫。<item name="android:windowSplashScreenAnimatedIcon">@drawable/...</item>
使用
windowSplashScreenAnimationDuration
表示啟動畫面圖示動畫的持續時間。做此設定不會影響啟動畫面的實際顯示時間。不過,您可以在使用SplashScreenView#getIconAnimationDuration
自訂啟動畫面結束動畫時進行擷取。請參閱下一節的延長啟動畫面的停留時間部分,瞭解更多資訊。<item name="android:windowSplashScreenAnimationDuration">1000</item>
使用
windowSplashScreenIconBackgroundColor
來設定啟動畫面圖示後的背景。如果視窗背景與圖示之間的對比度不夠高,這項功能就能派上用場。<item name="android:windowSplashScreenIconBackgroundColor">@color/...</item>
或者,您也可以使用
windowSplashScreenBrandingImage
來設定要顯示在啟動畫面底部的圖片。設計指南不建議使用品牌宣傳圖片。<item name="android:windowSplashScreenBrandingImage">@drawable/...</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 if the initial data is ready. return if (viewModel.isReady) { // The content is ready; start drawing. content.viewTreeObserver.removeOnPreDrawListener(this) true } else { // The content is not 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 if the initial data is ready. if (mViewModel.isReady()) { // The content is ready; start drawing. content.getViewTreeObserver().removeOnPreDrawListener(this); return true; } else { // The content is not 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; }