הוספת מסך פתיחה

אם באפליקציה שלך מוטמע מסך פתיחה בהתאמה אישית או שנעשה בה שימוש בעיצוב של מרכז האפליקציות, צריך להעביר את האפליקציה שלכם לספרייה SplashScreen, שזמינה ב-Jetpack, כדי לוודא מוצג כראוי בכל הגרסאות של 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 לנושא של הפעילות צריך להשתמש ב-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")
        }
    }
}

מקורות מידע נוספים

מידע נוסף על מסכי פתיחה באופן כללי ואיך אפשר להשתמש בהם באפליקציה.