Android Splash Screen Implemetation

依不同螢幕dpi使用不同尺寸圖檔的 Android 啟動畫面 (Splash Screen) 實作步驟

一、準備圖檔

製作不同尺寸的圖,檔名皆為 splash.png,依 Android 規範存於各 dpi 目錄中,如下圖:

製作不同解析度的圖,檔名皆為 splash.png,依 Android 規範存於各 dpi 目錄中

建立新專案並匯入圖檔

Android Studio 建立新專案

Resouce Manager → → Import Drawables

選取所有目錄

自動對應,確認無誤後按 Next,下一畫面按 Import

完成後在 Project 中見到 drawable 下多了 splash

、建立 splash_background.xml

drawable 上點右鍵 New Drawable Resource Fils

File name: splash_background ; Root element: layer-list
之後按 OK

隨即建立一個 splash_background.xml 於 drawable 中

Splash_background.xml 中加入以下內容:

<item android:drawable="@color/white"/>

<item>

<bitmap android:gravity="center"

android:src="@drawable/splash"/>

</item>

@drawable/splash 即剛剛匯入之圖檔

splash_background.xml 完整程式

、於 themes.xml 中建立 SplashTheme style

於 Project values themes 中找到 themes.xml

於 themes.xml 中加入以下內容:

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">

<item name="android:windowBackground">@drawable/splash_background</item>

<item name="android:windowFullscreen">true</item>

</style>

@drawable/splash_background 即剛剛新增之 splash_background.xml

themes.xml 完整程式

、建立 SplashScreenActivity

於 Project java com.example.splashdeno 點右鍵
New Activity Empty Activity

Activity name: SplashScreenActivity ; Layout Name: activity_splash_screen
之後按 Finish

自動產生 SplashScreenActivity.kt 與 aciivity_splash_screen.xml 兩個檔案

、至 AndroidManifest.xml 中為 SplashScreenActivity 加入 SplashTheme style

於 Project manifests 中打開 AndroidManifest.xml
見到其中已自動新增 SplashScreenActivity

將 SplashScreenActivity 加入 SplashTheme style,並修改為開啟後第一個 Activity

<activity

android:name=".SplashScreenActivity"

android:exported="true"

android:theme="@style/SplashTheme">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

<meta-data

android:name="android.app.lib_name"

android:value="" />

</activity>

並將 MainActivity 中的 intent-filter 刪除

<activity

android:name=".MainActivity"

android:exported="true">


<meta-data

android:name="android.app.lib_name"

android:value="" />

</activity>

SplashScreenActivity.kt 加入程式

使用到 Handler 來設定時間,先加入:

private val handler = Handler()

注意:若沒有自動 import os.Handler 請手動加入

import android.os.Handler

Handler() 被劃橫槓,說是 Handler() 已不再使用,但沒有關係。

加入 Runnable callback function,當 Handler 執行完畢便開啟 MainActivity

private val runnable = Runnable {

if(!isFinishing) {

startActivity(Intent(this, MainActivity::class.java))

finish()

}

}

加入 onResume(),當 SplashScreenActivity 開啟時執行 postDelayed 2秒

override fun onResume() {

super.onResume()

handler.postDelayed(runnable, 2000)

}

加入 onPause(),當 SplashScreenActivity 結束時移除 callback function

override fun onPause() {

super.onPause()

handler.removeCallbacks(runnable)

}

SplashScreenActivity.kt 完整程式

測試

Build 的時候提示錯誤,說不認識 Intent,點 Import 即可解決

建立兩個不同解析度的模擬器

Pixel 3a 之結果

3.4 WQVGA 之結果

Splash 圖片都是出現2秒,之後進入 MainActivity 主畫面。上面紅色之註記 xxh 與 m 乃刻意於 splash.png 圖檔中加入以辨識使用何種尺寸之圖檔。

完成

參考資料:

  1. ANDROID FLY程式筆記 "Android Splash Screen 啟動畫面"及其影片

  2. Implementing the Perfect Splash Screen in Android

  3. 圖片來源:勞動部第 52 屆行動應用開發職類分區技能競賽試題題目素材