Android 12 應(yīng)用啟動動畫新功能

和你一起終身學(xué)習(xí),這里是程序員Android
經(jīng)典好文推薦,通過閱讀本文,您將收獲以下知識點:
一、啟動畫面
二、啟動畫面的工作原理
三、自定義應(yīng)用中的啟動畫面
四、 讓啟動畫面在屏幕上顯示更長時間
一、啟動畫面
Android 12 添加了 SplashScreen API,它可為所有應(yīng)用啟用新的應(yīng)用啟動動畫。這包括啟動時的進(jìn)入應(yīng)用運動、顯示應(yīng)用圖標(biāo)的啟動畫面,以及向應(yīng)用本身的過渡。

啟動畫面示例
這種新體驗可讓應(yīng)用每次啟動時都呈現(xiàn)標(biāo)準(zhǔn)設(shè)計元素,但它也可自定義,以便您的應(yīng)用能夠保持其獨特的品牌。
二、啟動畫面的工作原理
當(dāng)用戶啟動應(yīng)用而應(yīng)用的進(jìn)程未在運行(冷啟動)或 Activity 尚未創(chuàng)建(溫啟動)時,會發(fā)生以下事件。(在熱啟動期間從不顯示啟動畫面。)
系統(tǒng)使用主題以及您已定義的任何動畫顯示啟動畫面。
當(dāng)應(yīng)用準(zhǔn)備就緒時,會關(guān)閉啟動畫面并顯示應(yīng)用。
動畫的元素和機制
動畫的元素由 Android 清單中的 XML 資源文件定義。每個元素都有淺色模式和深色模式版本。
它們由窗口背景、動畫形式的應(yīng)用圖標(biāo)和圖標(biāo)背景組成:

啟動畫面的元素
關(guān)于這些元素,請注意以下幾點:
應(yīng)用圖標(biāo) (1) 應(yīng)該是矢量可繪制對象,它可以是靜態(tài)或動畫形式。雖然動畫的時長可以不受限制,但我們建議讓其不超過 1000 毫秒。默認(rèn)情況下,使用啟動器圖標(biāo)。
圖標(biāo)背景 (2) 是可選的,在圖標(biāo)與窗口背景之間需要更高的對比度時很有用。如果您使用一個自適應(yīng)圖標(biāo),當(dāng)該圖標(biāo)與窗口背景之間的對比度足夠高時,就會顯示其背景。
與自適應(yīng)圖標(biāo)一樣,前景的 ? 被遮蓋 (3)。
窗口背景 (4) 由不透明的單色組成。如果窗口背景已設(shè)置且為純色,則未設(shè)置相應(yīng)的屬性時默認(rèn)使用該背景。
啟動畫面動畫機制由進(jìn)入動畫和退出動畫組成。
進(jìn)入動畫由系統(tǒng)視圖到啟動畫面組成。這由系統(tǒng)控制且不可自定義。
退出動畫由隱藏啟動畫面的動畫運行組成。如果您要對其進(jìn)行自定義,您將可以訪問
SplashScreenView及其圖標(biāo),并且可以在它們之上運行任何動畫(需要設(shè)置轉(zhuǎn)換、不透明度和顏色)。在這種情況下,當(dāng)動畫完成時,需要手動移除啟動畫面。
三、自定義應(yīng)用中的啟動畫面
默認(rèn)情況下,SplashScreen 使用主題的 windowBackground(如果它是單色)和啟動器圖標(biāo)。啟動畫面的自定義通過向應(yīng)用主題添加屬性來完成。
您可以通過以下任一方式自定義應(yīng)用的啟動畫面:
設(shè)置主題屬性以更改其外觀
讓其在屏幕上顯示更長時間
自定義用于關(guān)閉啟動畫面的動畫
設(shè)置啟動畫面的主題以更改其外觀
您可以在 Activity 主題中指定以下屬性來自定義應(yīng)用的啟動畫面。如果您已有使用 android:windowBackground 等屬性的舊版啟動畫面實現(xiàn),不妨考慮為 Android 12 提供替代資源文件。
使用
windowSplashScreenBackground以特定的單色填充背景:
<item name="android:windowSplashScreenBackground">@color/...</item>
使用
windowSplashScreenAnimatedIcon替換起始窗口中心的圖標(biāo)。如果對象通過AnimationDrawable和AnimatedVectorDrawable可呈現(xiàn)動畫效果且可繪制,則也會在顯示起始窗口的同時播放動畫。
<item name="android:windowSplashScreenAnimatedIcon">@drawable/...</item>
使用
windowSplashScreenAnimationDuration設(shè)置啟動畫面在關(guān)閉之前顯示的時長。最長時間為 1000 毫秒。使用
windowSplashScreenIconBackground設(shè)置啟動畫面圖標(biāo)后面的背景。當(dāng)窗口背景與圖標(biāo)之間的對比度不夠高時,這很有用。
<item name=”android:windowSplashScreenIconBackground”>@color/...</item>
(可選)您可以使用
windowSplashScreenBrandingImage設(shè)置要顯示在啟動畫面底部的圖片。設(shè)計準(zhǔn)則建議不要使用品牌圖片。
<item name=”android:windowSplashScreenBrandingImage”>@drawable/...</item>
四、 讓啟動畫面在屏幕上顯示更長時間
當(dāng)應(yīng)用繪制第一幀后,啟動畫面會立即關(guān)閉。如果您需要從本地磁盤異步加載少量數(shù)據(jù)(如應(yīng)用內(nèi)主題設(shè)置),您可以使用 ViewTreeObserver.OnPreDrawListener 讓應(yīng)用暫停繪制第一幀。
// 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;
}
}
});
}
自定義用于關(guān)閉啟動畫面的動畫
您可以通過 Activity.getSplashScreen 進(jìn)一步自定義啟動畫面的動畫。
@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();
});
}
在此回調(diào)開始時,啟動畫面上動畫形式的矢量可繪制對象已經(jīng)開始。根據(jù)應(yīng)用啟動的時長,可繪制對象可能在其動畫的中間。使用 SplashScreenView.getIconAnimationStartMillis 可了解動畫何時開始。您可以按如下方式計算圖標(biāo)動畫的剩余時長:
// Get the duration of the animated vector drawable.
long animationDuration = splashScreenView.getIconAnimationDurationMillis();
// Get the start time of the animation.
long animationStart = splashScreenView.getIconAnimationStartMillis();
// Calculate the remaining duration of the animation.
long remainingDuration = Math.max(
animationDuration - (SystemClock.uptimeMillis() - animationStart),
0L
);友情推薦:
至此,本篇已結(jié)束。轉(zhuǎn)載網(wǎng)絡(luò)的文章,小編覺得很優(yōu)秀,歡迎點擊閱讀原文,支持原創(chuàng)作者,如有侵權(quán),懇請聯(lián)系小編刪除,歡迎您的建議與指正。同時期待您的關(guān)注,感謝您的閱讀,謝謝!
點擊閱讀原文,為大佬點贊!
