1. 程式人生 > >淺談Android介面繪製避免重複渲染

淺談Android介面繪製避免重複渲染

當你在Application中建立複雜的佈局時,頁面的渲染過程也變得更加緩慢。 此時,我們需要利用 <include />標籤(避免重複渲染)和 ViewStub類(延遲載入)來優化我們的頁面。 一、利用<include />標籤來避免重複渲染 當我們需要為App中的每個View都新增一個header或者footer時,你會怎麼做? 重複地複製貼上可以解決這個問題,但未免太繁雜。可以試著使用<include />標籤: 第一種方式,在<include />標籤內指定width及height: main.xml
  1. <RelativeLayout
    xmlns:android = "http://schemas.android.com/apk/res/android"
  2.     android:layout_width"fill_parent"
  3.     android:layout_height"fill_parent">
  4.     <Button
  5.         android:layout_width ="fill_parent"
  6.         android:layout_height ="wrap_content"
  7.         android:layout_gravity ="center_vertical"
  8.         android:onClick
     ="onShowMap"
  9.         android:text ="@string/show_map"/>
  10.     <include
  11.         android:layout_width ="fill_parent"
  12.         android:layout_height ="wrap_content"
  13.         android:layout_alignParentBottom ="true"
  14.         android:layout_marginBottom ="30dp"
  15.         layout ="@layout/footer"/>
  16. </RelativeLayout
    >

footer.xml
  1. <TextViewxmlns:android = "http://schemas.android.com/apk/res/android"
  2.     android:layout_width"0dp"
  3.     android:layout_height"0dp"
  4.     android:gravity"center"
  5.     android:text"@string/footer_text"/>

有個小細節需要注意,在footer.xml中,我們將width及height都設為0dp.目的是為了配合<include/>標籤中對width及height的定義 第二種方式,直接引用: main.xml
  1. <RelativeLayoutxmlns:android = "http://schemas.android.com/apk/res/android"
  2.     android:layout_width"fill_parent"
  3.     android:layout_height"fill_parent">
  4.     <Button
  5.         android:layout_width ="fill_parent"
  6.         android:layout_height ="wrap_content"
  7.         android:layout_gravity ="center_vertical"
  8.         android:onClick ="onShowMap"
  9.         android:text ="@string/show_map"/>
  10.     <includelayout ="@layout/footer"/>
  11. </RelativeLayout>

footer.xml
  1. <TextViewxmlns:android = "http://schemas.android.com/apk/res/android"
  2.     android:layout_width"fill_parent"
  3.     android:layout_height"wrap_content"
  4.     android:layout_alignParentBottom"true"
  5.     android:layout_marginBottom"30dp"
  6.     android:gravity"center"
  7.     android:text"@string/footer_text"/>

二、利用ViewStub類來延遲載入檢視 在設計檢視時,有時會考慮到某些檢視的可見性是依賴於使用者的操作或者執行裝置的具體環境的。 此時你會如何設計?僅僅是改變View的visible屬性? 我們先來看看ViewStub的介紹: ViewStub是一個不可見、不佔空間(zero-sized)的控制元件,它可以用來在執行時延遲載入檢視資源。只有當我們將ViewStub的可見性設為true,或者呼叫inflate()方法,它的檢視資源才會被載入。 假設我們設計的一個頁面中包含地圖View,試想一下以下這種情況:      有些使用者不需要看到地圖。 既然使用者不需要看到地圖,我們為何還堅持不懈地載入它?這反而影響了我們App的Performance。 此時,我們就可以利用ViewStub類了: main.xml
  1. <RelativeLayoutxmlns:android = "http://schemas.android.com/apk/res/android"
  2.     android:layout_width"fill_parent"
  3.     android:layout_height"fill_parent">
  4.     <Button
  5.         android:layout_width ="fill_parent"
  6.         android:layout_height ="wrap_content"
  7.         android:layout_gravity ="center_vertical"
  8.         android:onClick ="onShowMap"
  9.         android:text ="@string/show_map"/>
  10.     <ViewStub
  11.         android:id ="@+id/map_stub"
  12.         android:layout_width ="fill_parent"
  13.         android:layout_height ="fill_parent"
  14.         android:inflatedId ="@+id/map_view"
  15.         android:layout ="@layout/map"/>
  16. </RelativeLayout>

map.xml
  1. <com.google.android.maps.MapViewxmlns:android ="http://schemas.android.com/apk/res/android"
  2.     android:layout_width"fill_parent"
  3.     android:layout_height"fill_parent"
  4.     android:apiKey"my_api_key"
  5.     android:clickable"true"/>
接下來看看MainActivity
  1. publicclass MainActivity extends MapActivity {  
  2.   private View mViewStub;  
  3.   @Override
  4.   publicvoid onCreate (Bundle savedInstanceState ) {  
  5.     super. onCreate( savedInstanceState );  
  6.     setContentView( R. layout. main);  
  7.     mViewStub = findViewById( R. id. map_stub);  
  8.   }  
  9.   publicvoid onShowMap (View v) {  
  10.     mViewStub. setVisibility (View .VISIBLE );  
  11.   }  
  12. ....  
  13. }  

如你所見,在需要顯示影象時我們才呼叫onShowMap來改變map_stub的可見性。在改變之前,map_stub都不會渲染載入檢視資源。
小結:      1.當我們的頁面變得複雜,XML檔案內容過多時,<include />標籤可以有效地幫助我們整理檔案內容,同時提高了XML檔案的可讀性。同時,它的用法也與Fragment類似。      2.ViewStub是一個極佳的延遲載入檢視資源的方式。只要你設計的檢視是依賴於上下文來改變其可見性的,就利用ViewStub類吧。也許當你只將其應用在一個簡單的頁面當中時,並不會感覺到在效能上有任何提升,但是在複雜頁面中,它的效果是極佳的。