添加启动画面

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

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

添加依赖项

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

Groovy

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

Kotlin

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

请确保您使用的是 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 的主题(通常是定义启动器项或以其他方式导出的内容)替换为您在上一步创建的主题:

<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")
        }
    }
}

其他资源

不妨详细了解启动画面的一般概念,以及如何在应用中使用启动画面。