אם באפליקציה שלכם מוטמע מסך פתיחה בהתאמה אישית או נעשה שימוש בעיצוב של מרכז האפליקציות, אתם צריכים להעביר את האפליקציה לספרייה SplashScreen שזמינה ב-Jetpack, כדי לוודא שהיא מוצגת בצורה תקינה בכל הגרסאות של Wear OS.
בדף הזה מפורטות הוראות הטמעה שלב אחר שלב, שמסבירות איך להוסיף מסך פתיחה באמצעות הספרייה SplashScreen כך שהמסך יעמוד בהנחיות העיצוב.
הוספת יחסי תלות
מוסיפים את התלות הבאה לקובץ build.gradle של מודול האפליקציה:
Groovy
dependencies { implementation "androidx.core:core-splashscreen:1.2.0" }
Kotlin
dependencies { implementation("androidx.core:core-splashscreen:1.2.0") }
כדי לקבל תמיכה במאפיינים שמוגדרים כברירת מחדל ב-Wear OS, צריך לוודא שמשתמשים בגרסה 1.0.1 ואילך.
הוסף עיצוב
יוצרים עיצוב למסך פתיחה ב-res/values/styles.xml. רכיב ההורה תלוי בצורת הסמל:
- אם הסמל עגול, משתמשים ב-
Theme.SplashScreen. - אם לסמל יש צורה אחרת, משתמשים ב-
Theme.SplashScreen.IconBackground.
משתמשים ב-windowSplashScreenBackground כדי למלא את הרקע בצבע שחור אחיד. מגדירים את הערכים של postSplashScreenTheme לעיצוב שבו הפעילות צריכה להשתמש, ואת windowSplashScreenAnimatedIcon ל-drawable או ל-animated drawable:
<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"> <!-- Set a white background behind the splash screen icon. --> <item name="windowSplashScreenIconBackgroundColor">@android:color/white</item> </style>
שאר המאפיינים הם אופציונליים.
יצירת רכיב גרפי לעיצוב
עיצובים של מסכי פתיחה דורשים פריט שניתן לשרטוט כדי להעביר למאפיין windowSplashScreenAnimatedIcon. לדוגמה, אפשר ליצור אותו על ידי הוספת קובץ חדש res/drawable/splash_screen.xml ושימוש בסמל של מרכז האפליקציות ובגודל הנכון של סמל מסך הפתיחה:
<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 והוא משתנה בהתאם לצורה של הסמל:
<resources> <!-- Round app icon can take all of default space --> <dimen name="splash_screen_icon_size">48dp</dimen> </resources>
...או לא עגול, ולכן צריך להשתמש ברקע של הסמל:
<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 android:name=".snippets.SplashScreenActivity" android:exported="true" android:taskAffinity="" android:theme="@style/Theme.App.Starting"> <!-- ... --> </activity>
עדכון הפעילות הראשונית
מתקינים את מסך הפתיחה בפעילות ההתחלה לפני שקוראים ל-super.onCreate() וטוענים את הרכיב הקומפוזבילי:
class SplashScreenActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { installSplashScreen() super.onCreate(savedInstanceState) setContent { WearApp() } } }
מקורות מידע נוספים
מידע נוסף על מסכי פתיחה באופן כללי ואיך אפשר להשתמש בהם באפליקציה.
מומלץ בשבילך
- הערה: טקסט הקישור מוצג כש-JavaScript מושבת
- העברת ההטמעה של מסך הפתיחה ל-Android בגרסה 12 ואילך
- מסכי פתיחה
- שילוב של פעולות באפליקציה עם ווידג'טים של Android