Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 資訊

用 Theme 與 Style 解決 App 啟動閃黑屏問題

編輯:Android 資訊

前陣子老闆反應,裝置每次啟動 App 都會閃一下黑屏,客戶無法接受,只好想辦法處理,最後找到了下面這套方案。

先談成因。啟動 Activity 時,系統必須跑完 onCreate 與 onResume 才會把畫面顯示出來,也就是說,中間那段處理資料、解析版面的時間裡,視窗是空的,於是就露出黑色的背景。照這個思路想,把初始化工作降到最低是不是就能避免?實測之後發現並不然——就算 onCreate 裡什麼都不做,仍然會閃一下黑屏,因為解析並載入版面本身就佔掉一段時間。以下是可行的解法:

1. 自訂 Theme

設置背景圖Theme <style name="Theme.AppStartLoad" parent="android:Theme"> <item name="android:windowBackground">@drawable/ipod_bg</item> <item name="android:windowNoTitle">true</item> </style> //2、設置透明Theme <style name="Theme.AppStartLoadTranslucent" parent="android:Theme"> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> </style>

這裡定義了兩種 Theme。第一種指定一張背景圖,App 一啟動就先顯示這張圖,視覺上不會出現黑屏;第二種把視窗設為透明,啟動過程中畫面全透明,等版面初始化完成後再一次顯示出來。兩者的取捨如下:

  • Theme1:啟動看起來快,先出現背景圖,再刷其他元件,隱約有點不同步的感覺。
  • Theme2:讓人覺得啟動較慢,但畫面是一次性刷出,整體同步感更好。

2. 修改 AndroidManifest.xml

要讓上面的 Theme 生效,需要為對應的 Activity 指定 theme:

<application android:allowBackup="true" android:icon="@drawable/ipod_icon" android:label="@string/app_name" android:launchMode="singleTask"> <!-- iPod主界面 --> <activity android:name="com.apical.apicalipod.IPodMainActivity" <!-- 使用上面定義的樣式 mythou--> android:theme="@style/Theme.AppStartLoad" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> //...... </application>
  • 可以在個別 Activity 上套用自訂樣式;若寫在 application 節點上則是全域效果。
  • 自訂 Theme 放在 /res/values/styles.xml 中,若專案裡沒有這個檔案,自行新增一個即可。
  • 若 App 有多個 Activity 互相切換,切換的瞬間也可能出現短暫黑屏,原因同樣是版面初始化需要時間。想避免的話,在會被切換到的 Activity 上也加上這組樣式。
  • 兩種樣式都能消除黑屏,實際跑一下挑選自己喜歡的效果。
  • 要注意的是,這只是把黑屏「遮掉」,若初始化流程本身很慢,使用者仍會覺得 App 啟動慢,該優化的地方還是要優化。

3. 常用 Theme 屬性對照

android:theme="@android:style/Theme.Dialog" //Activity顯示為對話框模式 android:theme="@android:style/Theme.NoTitleBar" //不顯示應用程序標題欄 android:theme="@android:style/Theme.NoTitleBar.Fullscreen" //不顯示應用程序標題欄,並全屏 android:theme="Theme.Light " //背景為白色 android:theme="Theme.Light.NoTitleBar" //白色背景並無標題欄 android:theme="Theme.Light.NoTitleBar.Fullscreen" //白色背景,無標題欄,全屏 android:theme="Theme.Black" //背景黑色 android:theme="Theme.Black.NoTitleBar" //黑色背景並無標題欄 android:theme="Theme.Black.NoTitleBar.Fullscreen" //黑色背景,無標題欄,全屏 android:theme="Theme.Wallpaper" //用系統桌面為應用程序背景 android:theme="Theme.Wallpaper.NoTitleBar" //用系統桌面為應用程序背景,且無標題欄 android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" //用系統桌面為應用程序背景,無標題欄,全屏 android:theme="Theme.Translucent" //透明背景 android:theme="Theme.Translucent.NoTitleBar" //透明背景並無標題 android:theme="Theme.Translucent.NoTitleBar.Fullscreen" //透明背景並無標題,全屏 android:theme="Theme.Panel " //面板風格顯示 android:theme="Theme.Light.Panel" //平板風格顯示

4. Theme 與 Style 的差別

Android 中除了 Theme 還有 Style,例如下面這段是 Launcher 用來設定工作區圖示的 Style:

<style name="WorkspaceIcon"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">match_parent</item> <item name="android:layout_gravity">center</item> <item name="android:gravity">center_horizontal</item> <item name="android:singleLine">true</item> <item name="android:ellipsize">marquee</item> <item name="android:textSize">12sp</item> <item name="android:textColor">#FFF</item> <item name="android:shadowRadius">2.0</item> <item name="android:shadowColor">#B0000000</item> </style>

兩者的差異可以這樣理解:

  • Style 作用在單一 View 上,例如 Button、TextView。
  • Theme 透過 AndroidManifest.xml 的 application 或 activity 節點套用,作用範圍是整个應用或某個 Activity,具有全域性影響。
  • 若某個 View 同時被 Theme 與自己的 Style 影響,當屬性衝突時,Style 的優先級高於 Theme。

以上就是用 Theme 消除啟動閃黑屏的完整做法,順帶釐清了 Theme 與 Style 的關係。同樣的技巧稍作變化,也能拿來做啟動歡迎頁。不過說到底,使用者最在意的仍是啟動速度,因此持續優化初始化流程才是根本之道。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved