添加启动画面

如果您的应用实现自定义启动画面或使用启动器主题,请将您的应用迁移到 Jetpack 中提供的 SplashScreen 库,以确保它在所有 Wear OS 版本上正确显示。

请参阅本页面上的分步实现说明,了解如何使用 SplashScreen 库添加启动画面,以使屏幕符合设计指南

添加依赖项

将以下依赖项添加到您的应用模块的 build.gradle 文件中

Groovy

dependencies {
    implementation "androidx.core:core-splashscreen:1.2.0-beta02"
}

Kotlin

dependencies {
    implementation("androidx.core:core-splashscreen:1.2.0-beta02")
}

确保您使用的是 1.0.1 或更高版本,以获得对默认 Wear OS 尺寸的支持。

添加主题

res/values/styles.xml 中创建启动画面主题。父元素取决于图标的形状

  • 如果图标是圆形的,请使用 Theme.SplashScreen
  • 如果图标是其他形状,请使用 Theme.SplashScreen.IconBackground

使用 windowSplashScreenBackground 将背景填充为单一黑色。将 postSplashScreenTheme 的值设置为 Activity 应使用的主题,将 windowSplashScreenAnimatedIcon 的值设置为可绘制对象或动画可绘制对象

<resources>
    <style name="Theme.App" parent="@android:style/Theme.DeviceDefault" />

    <style name="Theme.App.Starting" parent="Theme.SplashScreen">
        <!-- Set the splash screen background to black -->
        <item name="windowSplashScreenBackground">@android:color/black</item>
        <!-- Use windowSplashScreenAnimatedIcon to add a drawable or an animated
             drawable. -->
        <item name="windowSplashScreenAnimatedIcon">@drawable/splash_screen</item>
        <!-- Set the theme of the Activity that follows your splash screen. -->
        <item name="postSplashScreenTheme">@style/Theme.App</item>
    </style>
</resources>

如果您使用非圆形图标,则需要在图标下方设置白色背景色。在这种情况下,请使用 Theme.SplashScreen.IconBackground 作为父主题,并设置 windowSplashScreenIconBackgroundColor 属性

<style name="Theme.App.Starting" parent="Theme.SplashScreen.IconBackground">
    ...
    <!-- Set a white background behind the splash screen icon. -->
    <item name="windowSplashScreenIconBackgroundColor">@android:color/white</item>
</style>

其他属性是可选的。

为主题创建可绘制对象

启动画面主题需要一个可绘制对象,以传递给 windowSplashScreenAnimatedIcon 属性。例如,您可以通过添加新文件 res/drawable/splash_screen.xml 并使用应用启动器图标和正确的启动画面图标大小来创建它

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:width="@dimen/splash_screen_icon_size"
        android:height="@dimen/splash_screen_icon_size"
        android:drawable="@mipmap/ic_launcher"
        android:gravity="center" />
</layer-list>

启动画面图标大小在 res/values/dimens.xml 中定义,并且根据图标是否为圆形而有所不同

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Round app icon can take all of default space -->
    <dimen name="splash_screen_icon_size">48dp</dimen>
</resources>

...或非圆形,因此必须使用图标背景

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Non-round icon with background must use reduced size to fit circle -->
    <dimen name="splash_screen_icon_size">36dp</dimen>
</resources>

指定主题

在您应用的清单文件 (AndroidManifest.xml) 中,将起始 Activity(通常是定义启动器项或以其他方式导出的 Activity)的主题替换为您在上一步中创建的主题

<manifest>
    <application android:theme="@style/Theme.App.Starting">
       <!-- or -->
       <activity android:theme="@style/Theme.App.Starting">
          <!-- ... -->
</manifest>

更新您的起始 Activity

在调用 super.onCreate() 之前,在起始 Activity 中安装您的启动画面

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Handle the splash screen transition.
        installSplashScreen()

        super.onCreate(savedInstanceState)
        setContent {
            WearApp("Wear OS app")
        }
    }
}

其他资源

了解有关启动画面的更多信息,以及如何在您的应用中使用它们。