編輯:Android 開發實例
這個問題大概每個人都被客戶反饋過:點開 App 的瞬間會先閃一下黑屏,然後才出現界面。使用者看起來就像程式卡了一下,觀感很差。
先談原因。Android 啟動一個 Activity 時,必須跑完 onCreate() 與 onResume(),視窗才會真正被繪製出來。也就是說,中間這段「解析版面、初始化資料」的空窗期,系統只能拿預設的黑色背景頂著。既然如此,直覺反應是「把 onCreate 裡的初始化都拿掉不就好了?」——實測之後你會發現,就算 onCreate 什麼都不做,黑屏依然會閃一下,因為單是 inflate 版面配置就需要時間。
所以正確思路不是「加快初始化」,而是「把空窗期填滿」。以下兩個 Theme 方案就是這個概念。
<style name="Theme.AppStartLoad" parent="android:Theme">
<item name="android:windowBackground">@drawable/ipod_bg</item>
<item name="android:windowNoTitle">true</item>
</style>
程式一啟動就先顯示這張背景圖,使用者看到的是品牌圖而不是黑屏,等界面準備好再換掉。
<style name="Theme.AppStartLoadTranslucent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
</style>
啟動後視窗是全透明的,畫面會等到內容都準備好才一次顯示出來,完全沒有「先黑一下」的過程。
定義好的樣式必須掛到 Activity 上才有用:
<application
android:allowBackup="true"
android:icon="@drawable/ipod_icon"
android:label="@string/app_name"
android:launchMode="singleTask">
<!-- 主界面 -->
<activity
android:name="com.apical.apicalipod.IPodMainActivity"
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>
幾個實務提醒:
<application> 上產生全域效果。/res/values/styles.xml,專案裡沒有這個檔案就自己新增一個。android:theme="@android:style/Theme.Dialog" // 對話框風格 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" // 淺色面板風格
Android 裡除了 Theme,還有 Style。以 Launcher 為例,它就曾用一個名為 WorkspaceIcon 的 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>
可以這樣理解兩者的關係:
AndroidManifest.xml 的 <application> 或 <activity> 套用,影響範圍是整個應用或整個 Activity,具有全域性。用 Theme 解決黑屏只是其中一個應用。掌握 Theme 與 Style 之後,歡迎頁、品牌啟動畫面、深色模式切換等需求都能用同一套思路完成。當然,最根本的功課還是持續優化初始化流程——視覺技巧能掩蓋問題,但掩蓋不等於解決。
Android 6.0 指紋識別開發實例:FingerprintManager 註冊流程
Android 6.0 之後,指紋識別可由 FingerprintManager 提供支援。本文以高通 64 位平台與 fpc1020 為例,說明如何撰寫指紋註冊畫面,並
第三方 App 接入微信平台實研究(二):通訊時序與回調機制
微信開放平台推出後,大量第三方應用希望接入其中,原因不難理解:能直接借用微信既有的社交關係鏈來傳播自己的內容,省下自建社交體系的大量成本。我在實習期間研究了這套接入流程,
Android記事本範例剖析(二):選項、上下文與動態選單
本篇繼續以記事本範例說明 Android 的選單系統。Android 主要提供三種選單:選項選單、上下文選單與子選單。理解它們的呼叫時機,是做出順手操作介面的基礎。 An
Android 解析 JSON 資料:從文字到可用欄位的完整流程
JSON(JavaScript Object Notation)是一種與程式語言無關的輕量資料交換格式,語法簡單、可讀性高,早已成為 XML 之外的主流選擇。本章示範如何