在 Android 應用程式中新增啟動畫面
啟動畫面(也稱為啟動載入畫面)會在 Android 應用程式載入時提供簡單的初始體驗。它們為您的應用程式做好準備,同時允許應用程式引擎載入並初始化您的應用程式。
總覽
#在 Android 中,您可以控制兩個不同的畫面:一個是您的 Android 應用程式初始化時顯示的啟動載入畫面,另一個是 Flutter 體驗初始化時顯示的啟動畫面。
初始化應用程式
#每個 Android 應用程式都需要初始化時間,同時作業系統會設定應用程式的處理程序。Android 提供啟動載入畫面的概念,以在應用程式初始化時顯示 Drawable
。
Drawable
是一種 Android 圖形。若要了解如何在 Android Studio 中將 Drawable
新增到您的 Flutter 專案中,請查看 Android 開發人員文件中的將可繪製資源匯入專案。
預設的 Flutter 專案範本包含啟動主題和啟動背景的定義。您可以透過編輯 styles.xml
來進行自訂,您可以在其中定義一個主題,其 windowBackground
設定為應顯示為啟動載入畫面的 Drawable
。
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
<item name="android:windowBackground">@drawable/launch_background</item>
</style>
此外,styles.xml
定義了在啟動載入畫面消失後應用於 FlutterActivity
的正常主題。正常主題背景僅在啟動畫面消失後,以及在方向變更和 Activity
還原期間短暫顯示。因此,建議正常主題使用與 Flutter UI 的主要背景顏色相似的純色背景顏色。
<style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
<item name="android:windowBackground">@drawable/normal_background</item>
</style>
在 AndroidManifest.xml 中設定 FlutterActivity
#在 AndroidManifest.xml
中,將 FlutterActivity
的 theme
設定為啟動主題。然後,將中繼資料元素新增到所需的 FlutterActivity
,以指示 Flutter 在適當的時間從啟動主題切換到正常主題。
<activity
android:name=".MyActivity"
android:theme="@style/LaunchTheme"
// ...
>
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
現在,Android 應用程式會在應用程式初始化時顯示所需的啟動載入畫面。
Android 12
#若要在 Android 12 上設定您的啟動畫面,請查看Android 啟動畫面。
從 Android 12 開始,您必須在 styles.xml
檔案中使用新的啟動畫面 API。請考慮為 Android 12 和更高版本建立替代資源檔案。同時請確保您的背景圖片符合圖示準則;如需更多詳細資訊,請查看Android 啟動畫面。
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
<item name="android:windowSplashScreenBackground">@color/bgColor</item>
<item name="android:windowSplashScreenAnimatedIcon">@drawable/launch_background</item>
</style>
請確保您的資訊清單中未設定 io.flutter.embedding.android.SplashScreenDrawable
,並且未實作 provideSplashScreen
,因為這些 API 已被棄用。這樣做會導致 Android 啟動載入畫面在應用程式啟動時平滑淡入 Flutter,且應用程式可能會當機。
某些應用程式可能希望在 Flutter 中繼續顯示 Android 啟動載入畫面的最後一個影格。例如,這會保留單一影格的錯覺,同時在 Dart 中繼續進行額外的載入。若要達到此目的,下列 Android API 可能會有所幫助
import android.os.Build
import android.os.Bundle
import androidx.core.view.WindowCompat
import io.flutter.embedding.android.FlutterActivity
class MainActivity : FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
// Aligns the Flutter view vertically with the window.
WindowCompat.setDecorFitsSystemWindows(getWindow(), false)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Disable the Android splash screen fade out animation to avoid
// a flicker before the similar frame is drawn in Flutter.
splashScreen.setOnExitAnimationListener { splashScreenView -> splashScreenView.remove() }
}
super.onCreate(savedInstanceState)
}
}
import android.os.Build;
import android.os.Bundle;
import android.window.SplashScreenView;
import androidx.core.view.WindowCompat;
import io.flutter.embedding.android.FlutterActivity;
public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// Aligns the Flutter view vertically with the window.
WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Disable the Android splash screen fade out animation to avoid
// a flicker before the similar frame is drawn in Flutter.
getSplashScreen()
.setOnExitAnimationListener(
(SplashScreenView splashScreenView) -> {
splashScreenView.remove();
});
}
super.onCreate(savedInstanceState);
}
}
然後,您可以在 Flutter 中重新實作第一個影格,該影格會在螢幕上的相同位置顯示 Android 啟動載入畫面的元素。若要查看範例,請查看Android 啟動畫面範例應用程式。
除非另有說明,否則本網站上的文件反映了 Flutter 的最新穩定版本。頁面上次更新於 2024-06-26。 檢視來源 或 回報問題。