編輯: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 一啟動就先顯示這張圖,視覺上不會出現黑屏;第二種把視窗設為透明,啟動過程中畫面全透明,等版面初始化完成後再一次顯示出來。兩者的取捨如下:
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>
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>
兩者的差異可以這樣理解:
以上就是用 Theme 消除啟動閃黑屏的完整做法,順帶釐清了 Theme 與 Style 的關係。同樣的技巧稍作變化,也能拿來做啟動歡迎頁。不過說到底,使用者最在意的仍是啟動速度,因此持續優化初始化流程才是根本之道。
Android MVVM 應用框架構建完全指南
概述 一提到 Android MVVM,相信很多人第一時間都會想到 Google 在 2015 年推出的 DataBinding 框架。但必須強調的是,兩者的概念並不相同
7 款 Android 開發者常用的 UI 元件
本文由碼農網 – 小峰原創,轉載請看清文末的轉載要求,歡迎參與我們的付費投稿計劃! Android 開發是目前最熱門的行動開發技術之一。隨著開發者不斷努力與 Androi
LoonAndroid 開發框架功能詳解
LoonAndroid 是一款以減少樣板程式、降低耦合度為目標的 Android 開發框架。它與傳統 IOC 框架的思維有些不同,更接近透過攔截 Activity 生命週
Android 手勢密碼解鎖庫:開源實作與核心原理拆解
本文由碼農網 – 蘇耀東原創,轉載請看清文末的轉載要求,歡迎參與我們的付費投稿計劃! 簡介 本文完整介紹一個 Android 手勢密碼開源庫的使用方式與實現細節。這個庫主