天天看點

Android開發之解決APP啟動白屏或者黑屏閃現的問題

在做搜芽的過程中,發現那個外包人緣做的不行,因為啟動的時候會停頓,然後白屏一會,聯想到幾個月前我在我的三僚企業通信軟體裡面拉起9k-mail的時候也會黑屏,是以決定學習一下。解決一下。這不,萬能的網絡還是非常有用的。

在這裡總結一下。

(參考及轉載:http://www.2cto.com/kf/201409/339293.html)

歡迎頁啟動的線程由于請求和處理的資料量過大而,導緻歡迎頁在出現之前界面上會有一個短暫的白色閃屏停留,當然白色閃屏的停留是因為 application 的主題樣式android:theme=@style/apptheme 使用了 theme.light 題導緻的,light 樣式的 windowbackground、colorbackground、colorforeground 等屬性的值均為 light 也就是白色偏亮,是以才會出現白色閃屏。下面是我的 app 出現白色閃屏時樣式引用的代碼:

<a target="_blank" href="http://www.2cto.com/kf/201409/339293.html#">?</a>

1

<code>&lt;style name=</code><code>"apptheme"</code><code>parent=</code><code>"android:theme.light"</code><code>type=</code><code>"text/css"</code><code>&gt;&lt;br&gt;&lt;br&gt;  &lt;/style&gt;</code>

簡單的修改後,閃屏顔色為黑色,代碼如下:

<code>&lt;style name=</code><code>"apptheme"</code><code>parent=</code><code>"android:style/theme.black.notitlebar.fullscreen"</code><code>type=</code><code>"text/css"</code><code>&gt;  &lt;/style&gt;</code>

代碼修改後引用的樣式為黑色主題,但歡迎頁仍然會有黑色閃屏短暫的停留。繼續進行修改,設定透明屬性為 true,代碼如下:

<code>&lt;style name=</code><code>"apptheme"</code><code>parent=</code><code>"android:style/theme.black.notitlebar.fullscreen"</code><code>type=</code><code>"text/css"</code><code>&gt;&lt;item name=android:windowistranslucent&gt;</code><code>true</code><code>&lt;/item&gt;&lt;/style&gt;</code>

經過這次的修改之後黑色閃屏現象消失了,最終達到了自己理想的效果。最後,經過查閱資料發現已經有人總結和處理過這類問題了,并且給出了優缺點的分析,我在這裡以我的了解對其進行引用。

原來避免黑色閃屏有2種方法,分别為:1.為 theme 設定背景圖;2.為 theme 設定透明屬性。顯然我采用的是第二種方式,先分别看看這2種方式所引用的代碼:

<code>&lt;!-- 為 theme 設定背景圖 --&gt;&lt;style name=</code><code>"apptheme"</code><code>parent=</code><code>"android:style/theme.black.notitlebar.fullscreen"</code><code>type=</code><code>"text/css"</code><code>&gt;&lt;item name=android:windowbackground&gt;</code><code>@drawable</code><code>/splash_bg&lt;/item&gt;&lt;/style&gt;</code>

<code>&lt;!-- 為 theme 設定透明屬性 --&gt;&lt;style name=</code><code>"apptheme"</code><code>parent=</code><code>"android:style/theme.black.notitlebar.fullscreen"</code><code>type=</code><code>"text/css"</code><code>&gt;&lt;item name=android:windowistranslucent&gt;</code><code>true</code><code>&lt;/item&gt;&lt;/style&gt;</code>

上面的2種 theme 中,為 theme 設定背景圖後程式在啟動的時候,會首先顯示這張圖,避免發生黑屏;為 theme 設定透明屬性,程式啟動後不會黑屏而是透明,等到界面初始化完成後才一次性顯示出來。下面是兩種方式的優缺點:

為 theme 設定背景圖 給人程式啟動快的感覺,界面先顯示背景圖,然後再重新整理其他界面控件,重新整理不同步。

為 theme 設定透明屬性 給人程式啟動慢的感覺,界面會一次性刷出來,重新整理同步。

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<code>&lt;style name=</code><code>"apptheme"</code><code>parent=</code><code>"appbasetheme"</code><code>type=</code><code>"text/css"</code><code>&gt;&lt;!-- &lt;item name=android:windowanimationstyle&gt;</code><code>@style</code><code>/animation.activity.style&lt;/item&gt; --&gt;</code>

<code>        </code><code>&lt;item name=android:windowanimationstyle&gt;</code><code>@style</code><code>/animation.activity.translucent.style&lt;/item&gt;</code>

<code>        </code><code>&lt;item name=android:windowbackground&gt;</code><code>@android</code><code>:color/transparent&lt;/item&gt;</code>

<code>        </code><code>&lt;item name=android:windowistranslucent&gt;</code><code>true</code><code>&lt;/item&gt;&lt;/style&gt;&lt;style name=</code><code>"animation.activity.style"</code><code>parent=</code><code>"@android:style/animation.activity"</code><code>type=</code><code>"text/css"</code><code>&gt;&lt;item name=android:activityopenenteranimation&gt;</code><code>@anim</code><code>/base_slide_right_in&lt;/item&gt;</code>

<code>        </code><code>&lt;item name=android:activityopenexitanimation&gt;</code><code>@anim</code><code>/base_stay_orig&lt;/item&gt;</code>

<code>        </code><code>&lt;item name=android:activitycloseenteranimation&gt;</code><code>@anim</code><code>/base_stay_orig&lt;/item&gt;</code>

<code>        </code><code>&lt;item name=android:activitycloseexitanimation&gt;</code><code>@anim</code><code>/base_slide_right_out&lt;/item&gt;</code>

<code>        </code><code>&lt;item name=android:taskopenenteranimation&gt;</code><code>@anim</code><code>/base_slide_right_in&lt;/item&gt;</code>

<code>        </code><code>&lt;item name=android:taskopenexitanimation&gt;</code><code>@anim</code><code>/base_stay_orig&lt;/item&gt;</code>

<code>        </code><code>&lt;item name=android:taskcloseenteranimation&gt;</code><code>@anim</code><code>/base_stay_orig&lt;/item&gt;</code>

<code>        </code><code>&lt;item name=android:taskcloseexitanimation&gt;</code><code>@anim</code><code>/base_slide_right_out&lt;/item&gt;</code>

<code>        </code><code>&lt;item name=android:tasktofrontenteranimation&gt;</code><code>@anim</code><code>/base_slide_right_in&lt;/item&gt;</code>

<code>        </code><code>&lt;item name=android:tasktofrontexitanimation&gt;</code><code>@anim</code><code>/base_stay_orig&lt;/item&gt;</code>

<code>        </code><code>&lt;item name=android:tasktobackenteranimation&gt;</code><code>@anim</code><code>/base_stay_orig&lt;/item&gt;</code>

<code>        </code><code>&lt;item name=android:tasktobackexitanimation&gt;</code><code>@anim</code><code>/base_slide_right_out&lt;/item&gt;&lt;/style&gt;&lt;style name=</code><code>"animation.activity.translucent.style"</code><code>parent=</code><code>"@android:style/animation.translucent"</code><code>type=</code><code>"text/css"</code><code>&gt;&lt;item name=android:windowenteranimation&gt;</code><code>@anim</code><code>/base_slide_right_in&lt;/item&gt;</code>

<code>        </code><code>&lt;item name=android:windowexitanimation&gt;</code><code>@anim</code><code>/base_slide_right_out&lt;/item&gt;&lt;/style&gt;</code>

配置style繼承的parent為

<code>&lt;style name=</code><code>"animation.activity.translucent.style"</code><code>parent=</code><code>"@android:style/animation.translucent"</code><code>type=</code><code>"text/css"</code><code>&gt;&lt;item name=android:windowenteranimation&gt;</code><code>@anim</code><code>/base_slide_right_in&lt;/item&gt;</code>

然後讓apptheme的

<code>android:windowanimationstyle為上面的style</code>

<code>&lt;style name=</code><code>"apptheme"</code><code>parent=</code><code>"appbasetheme"</code><code>type=</code><code>"text/css"</code><code>&gt;&lt;item name=android:windowanimationstyle&gt;</code><code>@style</code><code>/animation.activity.translucent.style&lt;/item&gt;</code>

<code>        </code><code>&lt;item name=android:windowistranslucent&gt;</code><code>true</code><code>&lt;/item&gt;&lt;/style&gt;</code>

如果想在所有的activity切換時候使用該theme,可以配置application,也可以單個單個配置在activity裡面。

當然首頁退出的時候可以單獨配置mainactivity的退出動畫和進入動畫

<code>&lt;style name=</code><code>"animation.activity.translucent.style.main"</code><code>parent=</code><code>"@android:style/animation.translucent"</code><code>type=</code><code>"text/css"</code><code>&gt;&lt;item name=android:windowexitanimation&gt;</code><code>@anim</code><code>/slide_right_out&lt;/item&gt;&lt;/style&gt;</code>

mainactivity的退出和進入動畫可以引用系統提供的,但是好像在style裡面配置引用不了有些系統的anim,

在檔案夾sdkplatformsandroid-20data es下面,activity_open_enter.xml,activity_close_exit.xml可以直接拷貝到項目中,修改

單獨寫一個進入或者退出,然後其它預設。。。。。。。

多動手,測試,如果有錯誤的地方麻煩留言一起交流,謝謝

另一篇值得看的文章:(http://blog.csdn.net/u012970411/article/details/16981441)

前幾天boss就反應說,機器每次啟動程式都會閃一下黑屏,這個客戶不接受。沒辦法,隻能想想怎麼解決,最後找到了下面的方法。閃黑屏的原因主要是我們啟動activity的時候,需要跑完oncreate和onresume才會顯示界面。也就是說需要處理一些資料後,才會顯示。按照這種思路,是不是我把初始化的工作盡量減少就可以避免黑屏?事實是,就算你oncreate啥都不做,仍然會閃一下黑屏,因為初始化解析界面時需要一定時間。下面是解決辦法:

(ps:建立的qq群,有興趣可以加入一起讨論:android群:322599434)  

1、自定義theme

Android開發之解決APP啟動白屏或者黑屏閃現的問題
Android開發之解決APP啟動白屏或者黑屏閃現的問題

  上面我定義了兩種theme,第一種theme就是設定一張背景圖。當程式啟動時,首先顯示這張背景圖,避免出現黑屏。第二種theme是把樣式設定為透明,程式啟動後不會黑屏而是整個透明了,等到界面初始化完才一次性顯示出來。下面說說兩種方式的優缺點:

theme1 程式啟動快,界面先顯示背景圖,然後再重新整理其他界面控件。給人重新整理不同步感覺。

theme2 給人程式啟動慢感覺,界面一次性刷出來,重新整理同步。

2、修改androidmanifest.xml

為了使上面theme生效,我們需要設定一些activity的theme

Android開發之解決APP啟動白屏或者黑屏閃現的問題
Android開發之解決APP啟動白屏或者黑屏閃現的問題

可以在activity裡面增加上面自定義的樣式。另外在application裡面增加也是可以的,而且是全局效果。

自定義theme放在 /res/values/styles.xml 裡面。如果沒有這個檔案,自己添加一個即可。

如果存在多個activity切換,中間也可能會存在短暫黑屏問題。原因也是activity啟動的時候需要初始化加載資料,如果想避免這種情況,可以在你切換的activity裡面增加上面的樣式。

上面兩種樣式都可以避免黑屏。可以實際測試一下你的程式選擇一種效果。

這個隻是把黑屏避免了,但是如果你程式初始化啟動慢,還是會給人程式啟動慢的感覺。需要自行優化程式初始化過程。

 

3、theme屬性詳解

Android開發之解決APP啟動白屏或者黑屏閃現的問題
Android開發之解決APP啟動白屏或者黑屏閃現的問題

4、theme和style

  android裡面除了theme外還有style,例如下面是launcher裡面配置workspace的一個style

Android開發之解決APP啟動白屏或者黑屏閃現的問題
Android開發之解決APP啟動白屏或者黑屏閃現的問題

style可以了解為一組屬性集合,友善不同的view設定使用,我們在view裡面使用style的時候,跟使用theme是一樣的應用方法。那麼style和theme有什麼差別?下面列出兩者差別:

樣式用在單獨的view,如:button、textview等

主題通過androidmanifest.xml中的&lt;application&gt;和&lt;activity&gt;用在整個應用或者某個 activity,主題對整個應用或某個activity存在全局性影響。

如果一個應用使用了主題,同時應用下的view也使用了樣式,那麼當主題與樣式屬性發生沖突時,樣式的優先級高于主題。

  上面就是通過theme解決程式啟動閃黑屏問題,并且講解了theme和style,通過theme配置,其實還可以做個歡迎頁面。不過我們都希望程式啟動速度越快越好,是以還是需要多多優化自己的程式。

edited by mythou

繼續閱讀