为您的 Android 应用添加启动页 (Splash Screen)
了解如何为您的 Android 应用添加启动页。
概述
#启动页(也称为加载页)可以在 Android 应用加载时提供一个简单的初始体验。它为您的应用程序搭建了框架,同时为应用引擎的加载和应用初始化预留了时间。
您可以通过以下几种方式来实现启动页:
-
您可以使用 pub.dev 上提供的软件包。
-
您可以手动实现,如 splash screen 示例应用所示。本页剩余内容将基于手动实现的方式进行说明。
初始化应用
#每个 Android 应用在操作系统建立应用进程时都需要初始化时间。Android 提供了启动页 (launch screen) 的概念,以便在应用初始化时显示 Drawable。
Drawable 是一种 Android 图形资源。要了解如何在 Android Studio 中将 Drawable 添加到您的 Flutter 项目,请参阅 Android 开发者文档中的 将 Drawable 导入到项目中。
默认的 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 应用会在初始化时显示您设置的启动页。
SplashScreen API
#Android 12 引入了 SplashScreen API。请在您的 styles.xml 文件中使用 SplashScreen API。例如:
<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>
某些应用可能希望在 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 启动页的元素显示在屏幕的相同位置。有关示例,请查看 splash screen 示例应用。