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 开发者文档中的《将可绘制对象导入您的项目》。

默认的 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>

请确保您的清单中**未**设置 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 启动屏幕示例应用