淺談Android介面繪製避免重複渲染
阿新 • • 發佈:2019-01-30
當你在Application中建立複雜的佈局時,頁面的渲染過程也變得更加緩慢。
此時,我們需要利用 <include />標籤(避免重複渲染)和 ViewStub類(延遲載入)來優化我們的頁面。
一、利用<include />標籤來避免重複渲染
當我們需要為App中的每個View都新增一個header或者footer時,你會怎麼做?
重複地複製貼上可以解決這個問題,但未免太繁雜。可以試著使用<include />標籤:
第一種方式,在<include />標籤內指定width及height:
main.xml
footer.xml
有個小細節需要注意,在footer.xml中,我們將width及height都設為0dp.目的是為了配合<include/>標籤中對width及height的定義。 第二種方式,直接引用: main.xml
footer.xml
二、利用ViewStub類來延遲載入檢視 在設計檢視時,有時會考慮到某些檢視的可見性是依賴於使用者的操作或者執行裝置的具體環境的。 此時你會如何設計?僅僅是改變View的visible屬性? 我們先來看看ViewStub的介紹: ViewStub是一個不可見、不佔空間(zero-sized)的控制元件,它可以用來在執行時延遲載入檢視資源。只有當我們將ViewStub的可見性設為true,或者呼叫inflate()方法,它的檢視資源才會被載入。 假設我們設計的一個頁面中包含地圖View,試想一下以下這種情況: 有些使用者不需要看到地圖。 既然使用者不需要看到地圖,我們為何還堅持不懈地載入它?這反而影響了我們App的Performance。 此時,我們就可以利用ViewStub類了: main.xml
map.xml
如你所見,在需要顯示影象時我們才呼叫onShowMap來改變map_stub的可見性。在改變之前,map_stub都不會渲染載入檢視資源。
小結: 1.當我們的頁面變得複雜,XML檔案內容過多時,<include />標籤可以有效地幫助我們整理檔案內容,同時提高了XML檔案的可讀性。同時,它的用法也與Fragment類似。 2.ViewStub是一個極佳的延遲載入檢視資源的方式。只要你設計的檢視是依賴於上下文來改變其可見性的,就利用ViewStub類吧。也許當你只將其應用在一個簡單的頁面當中時,並不會感覺到在效能上有任何提升,但是在複雜頁面中,它的效果是極佳的。
-
<RelativeLayout
- 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
- 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
- <TextViewxmlns: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
- <RelativeLayoutxmlns: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"/>
- <includelayout ="@layout/footer"/>
- </RelativeLayout>
footer.xml
- <TextViewxmlns: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
- <RelativeLayoutxmlns: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.MapViewxmlns: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"/>
- publicclass MainActivity extends MapActivity {
- private View mViewStub;
- @Override
- publicvoid onCreate (Bundle savedInstanceState ) {
- super. onCreate( savedInstanceState );
- setContentView( R. layout. main);
- mViewStub = findViewById( R. id. map_stub);
- }
- publicvoid onShowMap (View v) {
- mViewStub. setVisibility (View .VISIBLE );
- }
- ....
- }
如你所見,在需要顯示影象時我們才呼叫onShowMap來改變map_stub的可見性。在改變之前,map_stub都不會渲染載入檢視資源。
小結: 1.當我們的頁面變得複雜,XML檔案內容過多時,<include />標籤可以有效地幫助我們整理檔案內容,同時提高了XML檔案的可讀性。同時,它的用法也與Fragment類似。 2.ViewStub是一個極佳的延遲載入檢視資源的方式。只要你設計的檢視是依賴於上下文來改變其可見性的,就利用ViewStub類吧。也許當你只將其應用在一個簡單的頁面當中時,並不會感覺到在效能上有任何提升,但是在複雜頁面中,它的效果是極佳的。