A graphic outlining the launch flow of an app including a splash screen

启动屏(也称为启动画面)可在您的 Android 应用加载时提供简单的初始体验。它们为您的应用奠定基础,同时允许应用引擎加载和您的应用初始化。

概述

#

在 Android 中,您可以控制两个独立的屏幕:一个在您的 Android 应用初始化时显示的启动画面,以及一个在 Flutter 体验初始化时显示的启动屏

初始化应用

#

每个 Android 应用都需要初始化时间,在此期间操作系统会设置应用的进程。Android 提供了启动画面的概念,用于在应用初始化时显示 Drawable

Drawable 是一个 Android 图形。要了解如何在 Android Studio 中将 Drawable 添加到 Flutter 项目,请参阅 Android 开发者文档中的将 drawable 导入到您的项目中

默认的 Flutter 项目模板包含启动主题和启动背景的定义。您可以通过编辑 styles.xml 来自定义此设置,您可以在其中定义一个主题,其 windowBackground 设置为应显示为启动画面的 Drawable

xml
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
    <item name="android:windowBackground">@drawable/launch_background</item>
</style>

此外,styles.xml 还定义了一个普通主题,该主题在启动画面消失后应用于 FlutterActivity。普通主题背景仅在启动屏消失后的极短时间内显示,并在方向更改和 Activity 恢复期间显示。因此,建议普通主题使用与 Flutter UI 主要背景颜色相似的纯色背景。

xml
<style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
    <item name="android:windowBackground">@drawable/normal_background</item>
</style>

在 AndroidManifest.xml 中设置 FlutterActivity

#

AndroidManifest.xml 中,将 FlutterActivitytheme 设置为启动主题。然后,向所需的 FlutterActivity 添加一个元数据元素,以指示 Flutter 在适当的时候从启动主题切换到普通主题。

xml
<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 启动屏以获取更多详细信息。

xml
<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>

请确保在您的 manifest 中设置 io.flutter.embedding.android.SplashScreenDrawable,并且实现 provideSplashScreen,因为这些 API 已被弃用。这样做会导致 Android 启动画面在应用启动时平滑地淡入 Flutter,并且应用可能会崩溃。

一些应用可能希望继续在 Flutter 中显示 Android 启动画面的最后一帧。例如,这可以在 Dart 中继续进行其他加载的同时保持单个帧的视觉效果。为此,以下 Android API 可能很有帮助

MainActivity.kt
kotlin
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)
  }
}
MainActivity.java
java
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 启动屏示例应用