スプラッシュ画面を追加する

アプリにカスタム スプラッシュ画面を実装するか、ランチャー テーマを使用する場合は、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")
}

デフォルトの Wear OS ディメンションのサポートを利用するには、バージョン 1.0.1 以降を使用してください。

テーマを追加する

スプラッシュ画面のテーマを res/values/styles.xml に作成します。親要素はアイコンの形状によって異なります。

  • アイコンが丸い場合は、Theme.SplashScreen を使用します。
  • アイコンの形状が異なる場合は、Theme.SplashScreen.IconBackground を使用します。

windowSplashScreenBackground を使用して、背景を黒一色で塗りつぶします。postSplashScreenTheme の値を、アクティビティで使用するテーマに設定し、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")
        }
    }
}

参考情報

スプラッシュ画面についての詳細と、アプリでの使用方法をご確認ください。