Splash Screen ※這個Demo建立當下環境為SDK34、kotlin version 1.8.10 **0.事前準備** 導入下列函式庫 ``` implementation("androidx.core:core-splashscreen:1.0.0") ``` **1.圖像資源** 按Android官方要求Splash Screen使用的圖標樣式需使用可繪製向量圖標建構 AnimatedVectorDrawable (AVD) 可是靜態或動畫的圖像資源 方便演示效果使用Vector圖像資源   這樣我們就有基本的圖像資源可做使用了 **2.建立Theme** 這邊建議單獨再創建一個資源當給Splash做使用 ``` <style name="Theme.App.Starting" parent="Theme.SplashScreen"> <!--背景色--> <item name="windowSplashScreenBackground">#E1BC85</item> <!--圖像資源--> <item name="windowSplashScreenAnimatedIcon">@drawable/baseline_local_gas_station_24</item> <!--持續時間--> <item name="windowSplashScreenAnimationDuration">2000</item> <!--Splash動畫結束要執行的主要樣式名稱--> <item name="postSplashScreenTheme">@style/Theme.KotilnDemo</item> </style> ``` 這邊將持續時間設為2000ms是為了看出效果,可以按照APP另作調整 值得一提的是**postSplashScreenTheme**這個屬性,當splash動畫結束時可使用這個屬性去另外套用你想使用的主要樣式名稱 以上是基本針對Splash動畫所設定的幾個屬性 **3.Manifast設定** 這邊將Manifast裡面的theme修改明成為我們剛剛建立的Theme即可  **4.程式碼設定**   反白的區塊為設定splash所需要的程式碼當App開始創建時 會執行**setKeepOnScreenCondition**在lambda內所要在ViewModel內工作的程式碼 這邊只有簡單變化Loading的boolean值,可依需要執行其他相關初始化 這邊不使用Flow使用Livedata應該也可行 **5.效果展示** https://drive.google.com/file/d/1Yzv_kQ_ZFzTTNGxtvO8Qlqk57tiEOkz4/view?usp=drive_link
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up