新增啟動畫面

如果您的應用程式實作自訂啟動畫面,或使用啟動器主題,請將應用程式遷移至 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) 中,將起始活動的主題 (通常是定義啟動器項目或以其他方式匯出的主題) 替換成您在上一個步驟中建立的主題。

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

更新起始活動

請先在啟動活動中安裝啟動畫面,然後再呼叫 super.onCreate()

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

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

其他資源

請點選這個連結,瞭解更多有關啟動畫面的一般概念,以及在應用程式中使用這類畫面的方法。