android UI 優化系之 使用theme 預加載

在很多時候,我們需要給一個Layout設置一個背景。例如,我們下下面的layout中使用了這樣一個背景: 6]dK,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"     
android:layout_width="fill_parent"    
android:layout_height="fill_parent"     
android:background="@drawable/antelope_canyon">
    <TextView android:text="@+id/TextView01"         
          android:id="@+id/TextView01"        
          android:layout_width="wrap_content"        
         android:layout_height="wrap_content" >  
   </TextView>

</LinearLayout>


其中的LinearLayout使用了 背景圖片antelope_canyon。整個程序的運行效果如下圖: 7E$ e1=
.~0A*a
vcsi @!
      仔細觀察程序的運行過過程,我們首先看到了黑色的activity背景,然後纔看到背景圖被加載,那是因爲在activity start以後,我們才能調用setContentView設置我們的layout,然後才繪製我們在layout中放置的背景圖。而在此之前,程序中繪製的是android中默認黑色背景。 UNcS\t2N
   這樣會給用戶感覺我們的activity啓動較慢。 Int 6xoz
x68$?CD
  然而,如果將背景圖定義在一個主題中,如下: d-g&TSGd

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.Droidus" parent="android:Theme">
        <item name="android:windowBackground">@drawable/antelope_canyon</item>
        <item name="android:windowNoTitle">true</item>
    </style>
</resources>


然後在activity中使用這個主題 qG=9zp4y?Y

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.droidus"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SpeedUpStartupActivity"
                  android:label="@string/app_name"
                  android:theme="@style/Theme.Droidus"
                   >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="4" />

</manifest> 


運行程序,可以看到背景圖馬上顯示了,沒有再看到黑色的背景圖。 g4<w6eB
[G^ir
爲什麼會有這樣的現象呢?那是因爲 程序的主題是在程序啓動的時候加載的,而不是在activity啓動之後加載! yw`xK2(C$
而如果在layout使用背景,背景圖是在activity啓動之後才加載,故而會讓用戶看到一個黑色背景閃動的過程。 >h/J{T(P>h

 

轉自:http://www.bangchui.org/read.php?tid=16

轉自:http://www.bangchui.org/read.php?tid=16

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章