50個Android開發技巧(02 消極式載入和避免重複渲染視圖)

來源:互聯網
上載者:User

當你在Application中建立複雜的布局時,頁面的渲染過程也變得更加緩慢。此時,我們需要利用 <include />標籤(避免重複渲染)和 ViewStub類(消極式載入)來最佳化我們的頁面。(原文地址:http://blog.csdn.net/vector_yi/article/details/24402101)
一、利用<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類吧。也許當你只將其應用在一個簡單的頁面當中時,並不會感覺到在效能上有任何提升,但是在複雜頁面中,它的效果是極佳的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.