50個Android開發技巧(02 延遲加載和避免重複渲染視圖)

當你在Application中建立複雜的佈局時,頁面的渲染過程也變得更加緩慢。
此時,咱們須要利用 <include />標籤(避免重複渲染)和 ViewStub類(延遲加載)來優化咱們的頁面。
1、利用<include />標籤來避免重複渲染

當咱們須要爲App中的每一個View都添加一個header或者footer時,你會怎麼作?
重複地複製粘貼能夠解決這個問題,但未免太繁雜。能夠試着使用<include />標籤:

第一種方式,在<include />標籤內指定width及height:
main.xml
[html]   view plain copy 在CODE上查看代碼片 派生到個人代碼片
  1. <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"  
  2.     android:layout_width"fill_parent"  
  3.     android:layout_height"fill_parent" >  
  4.   
  5.     <Button  
  6.         android:layout_width ="fill_parent"  
  7.         android:layout_height ="wrap_content"  
  8.         android:layout_gravity ="center_vertical"  
  9.         android:onClick ="onShowMap"  
  10.         android:text ="@string/show_map" />  
  11.   
  12.     <include  
  13.         android:layout_width ="fill_parent"  
  14.         android:layout_height ="wrap_content"  
  15.         android:layout_alignParentBottom ="true"  
  16.         android:layout_marginBottom ="30dp"  
  17.         layout ="@layout/footer" />  
  18.   
  19. </RelativeLayout>  

footer.xml
[html]   view plain copy 在CODE上查看代碼片 派生到個人代碼片
  1. <TextView xmlns: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
[html]   view plain copy 在CODE上查看代碼片 派生到個人代碼片
  1. <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"  
  2.     android:layout_width"fill_parent"  
  3.     android:layout_height"fill_parent" >  
  4.   
  5.     <Button  
  6.         android:layout_width ="fill_parent"  
  7.         android:layout_height ="wrap_content"  
  8.         android:layout_gravity ="center_vertical"  
  9.         android:onClick ="onShowMap"  
  10.         android:text ="@string/show_map" />  
  11.   
  12.     <include layout ="@layout/footer" />  
  13.   
  14. </RelativeLayout>  

footer.xml
[html]   view plain copy 在CODE上查看代碼片 派生到個人代碼片
  1. <TextView xmlns: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" />  

2、利用ViewStub類來延遲加載視圖
在設計視圖時,有時會考慮到某些視圖的可見性是依賴於用戶的操做或者運行設備的具體環境的。
此時你會如何設計?僅僅是改變View的visible屬性?
咱們先來看看ViewStub的介紹:
      ViewStub是一個不可見、不佔空間(zero-sized)的控件,它能夠用來在運行時延遲加載視圖資源。只有當咱們將ViewStub的可見性設爲true,或者調用inflate()方法,它的視圖資源纔會被加載。

假設咱們設計的一個頁面中包含地圖View,試想一下如下這種狀況:
     有些用戶不須要看到地圖。
既然用戶不須要看到地圖,咱們爲什麼還堅持不懈地加載它?這反而影響了咱們App的Performance。
此時,咱們就能夠利用ViewStub類了:
main.xml
[html]   view plain copy 在CODE上查看代碼片 派生到個人代碼片
  1. <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"  
  2.     android:layout_width"fill_parent"  
  3.     android:layout_height"fill_parent" >  
  4.   
  5.     <Button  
  6.         android:layout_width ="fill_parent"  
  7.         android:layout_height ="wrap_content"  
  8.         android:layout_gravity ="center_vertical"  
  9.         android:onClick ="onShowMap"  
  10.         android:text ="@string/show_map" />  
  11.   
  12.     <ViewStub  
  13.         android:id ="@+id/map_stub"  
  14.         android:layout_width ="fill_parent"  
  15.         android:layout_height ="fill_parent"  
  16.         android:inflatedId ="@+id/map_view"  
  17.         android:layout ="@layout/map" />  
  18. </RelativeLayout>  

map.xml
[html]   view plain copy 在CODE上查看代碼片 派生到個人代碼片
  1. <com.google.android.maps.MapView xmlns: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
[java]   view plain copy 在CODE上查看代碼片 派生到個人代碼片
  1. public class MainActivity extends MapActivity {  
  2.   
  3.   private View mViewStub;  
  4.   
  5.   @Override  
  6.   public void onCreate (Bundle savedInstanceState ) {  
  7.     super. onCreate( savedInstanceState );  
  8.     setContentView( R. layout. main);  
  9.     mViewStub = findViewById( R. id. map_stub);  
  10.   }  
  11.   
  12.   public void onShowMap (View v) {  
  13.     mViewStub. setVisibility (View .VISIBLE );  
  14.   }  
  15. ....  
  16. }  

如你所見,在須要顯示圖像時咱們才調用onShowMap來改變map_stub的可見性。在改變以前,map_stub都不會渲染加載視圖資源。

小結:
     1.當咱們的頁面變得複雜,XML文件內容過多時,<include />標籤能夠有效地幫助咱們整理文件內容,同時提升了XML文件的可讀性。同時,它的用法也與Fragment相似。
     2.ViewStub是一個極佳的延遲加載視圖資源的方式。只要你設計的視圖是依賴於上下文來改變其可見性的,就利用ViewStub類吧。也許當你只將其應用在一個簡單的頁面當中時,並不會感受到在性能上有任何提高,可是在複雜頁面中,它的效果是極佳的。
相關文章
相關標籤/搜索