android頁面渲染速度提升的常用方法


參考文檔:http://blog.csdn.net/vector_yi/article/details/24402101

 

當activity中用到的布局較多較為復雜時,頁面渲染就會變得復雜,現匯總以下常用方法,提升頁面加載速度。

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

當頁面中出現重復的布局時,如果只是復制粘貼,會顯得代碼陳余,並且繁瑣,使用<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" />

 

二、使用<merge/>標簽

如果引入的是由多個控件組成的布局文件,為了避免多重繪制,可以使用<merge/>標簽

merge標簽用來消除在include一個布局到另一個布局時所產生的冗余view group。比如現在很多布局中會有兩個連續的Button,於是我們將這兩個連續的Button做成可復用布局(re-usable layout)。在使用include標簽時我們必須先將這兩個Button用一個view group比如LinearLayout組織在一起然后供其它布局使用,如果是include的地方也是LiearLayout就會造成有兩層連續的LiearLayout,除了降低UI性能沒有任何好處。這個時候我們就可以使用<merge/>標簽作為可復用布局的root view來避免這個問題。

<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/add"/>

    <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/delete"/>

</merge>

當我們用<include/>標簽復用上述代碼時,系統會忽略merge元素,直接將兩個連續的Button放在<include/>標簽所在處。

 

三、利用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 ); } .... }
 
四:過度繪制檢查及處理
 
檢查過度繪制,如重復的parent view ,子控件重復的background等,導致加載時重復繪制。
 
 
 
如你所見,在需要顯示圖像時我們才調用onShowMap來改變map_stub的可見性。在改變之前,map_stub都不會渲染加載視圖資源。
 
小結:
     1.當我們的頁面變得復雜,XML文件內容過多時,<include />標簽可以有效地幫助我們整理文件內容,同時提高了XML文件的可讀性。同時,它的用法也與Fragment類似。
     2.ViewStub是一個極佳的延遲加載視圖資源的方式。只要你設計的視圖是依賴於上下文來改變其可見性的,就利用ViewStub類吧。也許當你只將其應用在一個簡單的頁面當中時,並不會感覺到在性能上有任何提升,但是在復雜頁面中,它的效果是極佳的。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM