前幾天Boss就反應(yīng)說,機(jī)器每次啟動(dòng)程序都會(huì)閃一下黑屏,這個(gè)客戶不接受。沒辦法,只能想想怎么解決,最后找到了下面的方法。閃黑屏的原因主要是我們啟動(dòng)Activity的時(shí)候,需要跑完onCreate和onResume才會(huì)顯示界面。也就是說需要處理一些數(shù)據(jù)后,才會(huì)顯示。按照這種思路,是不是我把初始化的工作盡量減少就可以避免黑屏?事實(shí)是,就算你onCreate啥都不做,仍然會(huì)閃一下黑屏,因?yàn)槌跏蓟馕鼋缑鏁r(shí)需要一定時(shí)間。下面是解決辦法:
1、自定義Theme
設(shè)置背景圖Theme
<style name="Theme.AppStartLoad" parent="android:Theme">
<item name="android:windowBackground">@drawable/ipod_bg</item>
<item name="android:windowNoTitle">true</item>
</style>
//2、設(shè)置透明Theme
<style name="Theme.AppStartLoadTranslucent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
</style>
上面我定義了兩種Theme,第一種Theme就是設(shè)置一張背景圖。當(dāng)程序啟動(dòng)時(shí),首先顯示這張背景圖,避免出現(xiàn)黑屏。第二種Theme是把樣式設(shè)置為透明,程序啟動(dòng)后不會(huì)黑屏而是整個(gè)透明了,等到界面初始化完才一次性顯示出來。
下面說說兩種方式的優(yōu)缺點(diǎn):
·Theme1 程序啟動(dòng)快,界面先顯示背景圖,然后再刷新其他界面控件。給人刷新不同步感覺。
·Theme2 給人程序啟動(dòng)慢感覺,界面一次性刷出來,刷新同步。
2、修改AndroidManifest.xml
為了使上面Theme生效,我們需要設(shè)置一些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 里面。如果沒有這個(gè)文件,自己添加一個(gè)即可。
·如果存在多個(gè)Activity切換,中間也可能會(huì)存在短暫黑屏問題。原因也是Activity啟動(dòng)的時(shí)候需要初始化加載數(shù)據(jù),如果想避免這種情況,可以在你切換的Activity里面增加上面的樣式。
·上面兩種樣式都可以避免黑屏??梢詫?shí)際測試一下你的程序選擇一種效果。
·這個(gè)只是把黑屏避免了,但是如果你程序初始化啟動(dòng)慢,還是會(huì)給人程序啟動(dòng)慢的感覺。需要自行優(yōu)化程序初始化過程。
3、Theme屬性詳解
android:theme="@android:style/Theme.Dialog" //Activity顯示為對話框模式
android:theme="@android:style/Theme.NoTitleBar" //不顯示應(yīng)用程序標(biāo)題欄
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" //不顯示應(yīng)用程序標(biāo)題欄,并全屏
android:theme="Theme.Light " //背景為白色
android:theme="Theme.Light.NoTitleBar" //白色背景并無標(biāo)題欄
android:theme="Theme.Light.NoTitleBar.Fullscreen" //白色背景,無標(biāo)題欄,全屏
android:theme="Theme.Black" //背景黑色
android:theme="Theme.Black.NoTitleBar" //黑色背景并無標(biāo)題欄
android:theme="Theme.Black.NoTitleBar.Fullscreen" //黑色背景,無標(biāo)題欄,全屏
android:theme="Theme.Wallpaper" //用系統(tǒng)桌面為應(yīng)用程序背景
android:theme="Theme.Wallpaper.NoTitleBar" //用系統(tǒng)桌面為應(yīng)用程序背景,且無標(biāo)題欄
android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" //用系統(tǒng)桌面為應(yīng)用程序背景,無標(biāo)題欄,全屏
android:theme="Theme.Translucent" //透明背景
android:theme="Theme.Translucent.NoTitleBar" //透明背景并無標(biāo)題
android:theme="Theme.Translucent.NoTitleBar.Fullscreen" //透明背景并無標(biāo)題,全屏
android:theme="Theme.Panel " //面板風(fēng)格顯示
android:theme="Theme.Light.Panel" //平板風(fēng)格顯示
4、Theme和Style
Android里面除了Theme外還有Style,例如下面是Launcher里面配置workspace的一個(gè)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設(shè)置使用,我們在View里面使用Style的時(shí)候,跟使用Theme是一樣的應(yīng)用方法。那么Style和Theme有什么區(qū)別?
下面列出兩者區(qū)別:
·樣式用在單獨(dú)的View,如:Button、TextView等
·主題通過AndroidManifest.xml中的<application>和<activity>用在整個(gè)應(yīng)用或者某個(gè) Activity,主題對整個(gè)應(yīng)用或某個(gè)Activity存在全局性影響。
·如果一個(gè)應(yīng)用使用了主題,同時(shí)應(yīng)用下的view也使用了樣式,那么當(dāng)主題與樣式屬性發(fā)生沖突時(shí),樣式的優(yōu)先級(jí)高于主題。
上面就是通過Theme解決程序啟動(dòng)閃黑屏問題,并且講解了Theme和Style,通過Theme配置,其實(shí)還可以做個(gè)歡迎頁面。不過我們都希望程序啟動(dòng)速度越快越好,因此還是需要多多優(yōu)化自己的程序。