Android開發技巧(延遲加載和避免重復渲染視圖)

jopen 9年前發布 | 41K 次閱讀 Android Android開發 移動開發

當你在Application中創建復雜的布局時,頁面的渲染過程也變得更加緩慢。

此時,我們需要利用 <include />標簽(避免重復渲染)和 ViewStub類(延遲加載)來優化我們的頁面。

一、利用<include />標簽來避免重復渲染

當我們需要為App中的每個View都添加一個header或者footer時,你會怎么做?
重復地復制粘貼可以解決這個問題,但未免太繁雜。可以試著使用<include />標簽:

第一種方式,在<include />標簽內指定width及height:
main.xml
    <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"  
        android:layout_width= "fill_parent"  
        android:layout_height= "fill_parent" >  

        <Button  
            android:layout_width ="fill_parent"  
            android:layout_height ="wrap_content"  
            android:layout_gravity ="center_vertical"  
            android:onClick ="onShowMap"  
            android:text ="@string/show_map" />  

        <include  
            android:layout_width ="fill_parent"  
            android:layout_height ="wrap_content"  
            android:layout_alignParentBottom ="true"  
            android:layout_marginBottom ="30dp"  
            layout ="@layout/footer" />  

    </RelativeLayout>  



footer.xml
    <TextView xmlns:android = "http://schemas.android.com/apk/res/android"  
        android:layout_width= "0dp"  
        android:layout_height= "0dp"  
        android:gravity= "center"  
        android:text= "@string/footer_text" />  


有個小細節需要注意,在footer.xml中,我們將width及height都設為0dp.目的是為了配合<include/>標簽中對width及height的定義

第二種方式,直接引用:
main.xml
    <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"  
        android:layout_width= "fill_parent"  
        android:layout_height= "fill_parent" >  

        <Button  
            android:layout_width ="fill_parent"  
            android:layout_height ="wrap_content"  
            android:layout_gravity ="center_vertical"  
            android:onClick ="onShowMap"  
            android:text ="@string/show_map" />  

        <include layout ="@layout/footer" />  

    </RelativeLayout>  


footer.xml
    <TextView xmlns:android = "http://schemas.android.com/apk/res/android"  
        android:layout_width= "fill_parent"  
        android:layout_height= "wrap_content"  
        android:layout_alignParentBottom= "true"  
        android:layout_marginBottom= "30dp"  
        android:gravity= "center"  
        android:text= "@string/footer_text" />  



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

假設我們設計的一個頁面中包含地圖View,試想一下以下這種情況:
     有些用戶不需要看到地圖。
既然用戶不需要看到地圖,我們為何還堅持不懈地加載它?這反而影響了我們App的Performance。
此時,我們就可以利用ViewStub類了:
main.xml
    <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"  
        android:layout_width= "fill_parent"  
        android:layout_height= "fill_parent" >  

        <Button  
            android:layout_width ="fill_parent"  
            android:layout_height ="wrap_content"  
            android:layout_gravity ="center_vertical"  
            android:onClick ="onShowMap"  
            android:text ="@string/show_map" />  

        <ViewStub  
            android:id ="@+id/map_stub"  
            android:layout_width ="fill_parent"  
            android:layout_height ="fill_parent"  
            android:inflatedId ="@+id/map_view"  
            android:layout ="@layout/map" />  
    </RelativeLayout>  


map.xml
    <com.google.android.maps.MapView xmlns:android ="http://schemas.android.com/apk/res/android"  
        android:layout_width= "fill_parent"  
        android:layout_height= "fill_parent"  
        android:apiKey= "my_api_key"  
        android:clickable= "true" />  

接下來看看MainActivity
    public class MainActivity extends MapActivity {  

      private View mViewStub;  

      @Override  
      public void onCreate (Bundle savedInstanceState ) {  
        super. onCreate( savedInstanceState );  
        setContentView( R. layout. main);  
        mViewStub = findViewById( R. id. map_stub);  
      }  

      public void onShowMap (View v) {  
        mViewStub. setVisibility (View .VISIBLE );  
      }  
    ....  
    }  


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

小結:
     1.當我們的頁面變得復雜,XML文件內容過多時,<include />標簽可以有效地幫助我們整理文件內容,同時提高了XML文件的可讀性。同時,它的用法也與Fragment類似。
     2.ViewStub是一個極佳的延遲加載視圖資源的方式。只要你設計的視圖是依賴于上下文來改變其可見性的,就利用ViewStub類吧。也許當你只將其應用在一個簡單的頁面當中時,并不會感覺到在性能上有任何提升,但是在復雜頁面中,它的效果是極佳的。
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!