Android 12 應(yīng)用啟動(dòng)動(dòng)畫(huà)新功能

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

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

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