和你一起終身學習,這裏是程序員 Android
經典好文推薦,通過閱讀本文,您將收穫以下知識點:
一、啓動畫面
二、啓動畫面的工作原理
三、自定義應用中的啓動畫面
四、 讓啓動畫面在屏幕上顯示更長時間
一、啓動畫面
Android 12 添加了 SplashScreen
API,它可爲所有應用啓用新的應用啓動動畫。這包括啓動時的進入應用運動、顯示應用圖標的啓動畫面,以及嚮應用本身的過渡。
這種新體驗可讓應用每次啓動時都呈現標準設計元素,但它也可自定義,以便您的應用能夠保持其獨特的品牌。
二、啓動畫面的工作原理
當用戶啓動應用而應用的進程未在運行(冷啓動)或 Activity 尚未創建(溫啓動)時,會發生以下事件。(在熱啓動期間從不顯示啓動畫面。)
系統使用主題以及您已定義的任何動畫顯示啓動畫面。
當應用準備就緒時,會關閉啓動畫面並顯示應用。
動畫的元素和機制
動畫的元素由 Android 清單中的 XML 資源文件定義。每個元素都有淺色模式和深色模式版本。
它們由窗口背景、動畫形式的應用圖標和圖標背景組成:
關於這些元素,請注意以下幾點:
應用圖標 (1) 應該是矢量可繪製對象,它可以是靜態或動畫形式。雖然動畫的時長可以不受限制,但我們建議讓其不超過 1000 毫秒。默認情況下,使用啓動器圖標。
圖標背景 (2) 是可選的,在圖標與窗口背景之間需要更高的對比度時很有用。如果您使用一個自適應圖標,當該圖標與窗口背景之間的對比度足夠高時,就會顯示其背景。
與自適應圖標一樣,前景的 ⅓ 被遮蓋 (3)。
窗口背景 (4) 由不透明的單色組成。如果窗口背景已設置且爲純色,則未設置相應的屬性時默認使用該背景。
啓動畫面動畫機制由進入動畫和退出動畫組成。
進入動畫由系統視圖到啓動畫面組成。這由系統控制且不可自定義。
退出動畫由隱藏啓動畫面的動畫運行組成。如果您要對其進行自定義,您將可以訪問
SplashScreenView
及其圖標,並且可以在它們之上運行任何動畫(需要設置轉換、不透明度和顏色)。在這種情況下,當動畫完成時,需要手動移除啓動畫面。
三、自定義應用中的啓動畫面
默認情況下,SplashScreen
使用主題的 windowBackground
(如果它是單色)和啓動器圖標。啓動畫面的自定義通過嚮應用主題添加屬性來完成。
您可以通過以下任一方式自定義應用的啓動畫面:
設置主題屬性以更改其外觀
讓其在屏幕上顯示更長時間
自定義用於關閉啓動畫面的動畫
設置啓動畫面的主題以更改其外觀
您可以在 Activity 主題中指定以下屬性來自定義應用的啓動畫面。如果您已有使用 android:windowBackground
等屬性的舊版啓動畫面實現,不妨考慮爲 Android 12 提供替代資源文件。
- 使用
windowSplashScreenBackground
以特定的單色填充背景:
<item name="android:windowSplashScreenBackground">@color/...</item>
- 使用
windowSplashScreenAnimatedIcon
替換起始窗口中心的圖標。如果對象通過AnimationDrawable
和AnimatedVectorDrawable
可呈現動畫效果且可繪製,則也會在顯示起始窗口的同時播放動畫。
<item name="android:windowSplashScreenAnimatedIcon">@drawable/...</item>
使用
windowSplashScreenAnimationDuration
設置啓動畫面在關閉之前顯示的時長。最長時間爲 1000 毫秒。使用
windowSplashScreenIconBackground
設置啓動畫面圖標後面的背景。當窗口背景與圖標之間的對比度不夠高時,這很有用。
<item name=”android:windowSplashScreenIconBackground”>@color/...</item>
- (可選)您可以使用
windowSplashScreenBrandingImage
設置要顯示在啓動畫面底部的圖片。設計準則建議不要使用品牌圖片。
<item name=”android:windowSplashScreenBrandingImage”>@drawable/...</item>
四、 讓啓動畫面在屏幕上顯示更長時間
當應用繪製第一幀後,啓動畫面會立即關閉。如果您需要從本地磁盤異步加載少量數據(如應用內主題設置),您可以使用 ViewTreeObserver.OnPreDrawListener
讓應用暫停繪製第一幀。
// 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
進一步自定義啓動畫面的動畫。
@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.getIconAnimationStartMillis
可瞭解動畫何時開始。您可以按如下方式計算圖標動畫的剩餘時長:
// 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
);
至此,本篇已結束。轉載網絡的文章,小編覺得很優秀,歡迎點擊閱讀原文,支持原創作者,如有侵權,懇請聯繫小編刪除,歡迎您的建議與指正。同時期待您的關注,感謝您的閱讀,謝謝!