編輯:關於 Android 程式設計
上一篇我們一起探討了Android中對SQLite數據庫操作的優化細節。今天,我們繼續Android性能優化系列——布局優化。
在Android中,UI布局作為對外的門面,其顯示速度直接影響著用戶對一個App的直觀印象。一個優秀的App,在布局與UI上往往都有不錯的性能優化,因此布局優化一直是Android性能優化中的重點之一。
為此,系統為我們提供了三個抽象的布局標籤:
(1)include
(2)viewstub
(3)merge
下面逐一介紹它們的作用與用法。
1> include
顧名思義,include標籤可以讓我們復用一個已有的layout布局。舉例來說,很多App在絕大部分場景下都是「topbar → content → footbar」這樣的設計,那麼我們就可以把topbar和footbar抽取為公共布局,用include來復用。代碼如下:
這是一個頭布局 layout_topbar.xml:
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="https://schemas.android.com/apk/res/android"> <RelativeLayout android:id="@+id/rl_root_title_base" android:layout_height="46dp" android:layout_width="match_parent"> <TextView android:gravity="center_vertical" android:id="@+id/tv_left_title" android:layout_centerVertical="true" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_width="wrap_content" android:textColor="#80C469"> <TextView android:ellipsize="end" android:id="@+id/tv_topic_title" android:layout_centerInParent="true" android:layout_height="wrap_content" android:layout_width="wrap_content" android:maxLength="10" android:singleLine="true" android:textColor="@color/cl_font_46" android:textSize="18dp"> <TextView android:gravity="center" android:id="@+id/tv_right_first_title" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:layout_width="wrap_content" android:minHeight="30dp" android:minWidth="30dp" android:textColor="@color/cl_80c469" android:textSize="16dp"> <TextView android:gravity="center" android:id="@+id/tv_right_second_title" android:layout_centerVertical="true" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:layout_toLeftOf="@id/tv_right_first_title" android:layout_width="wrap_content" android:minHeight="30dp" android:minWidth="30dp" android:textColor="#80C469"> </TextView></TextView></TextView></TextView></RelativeLayout> <View android:background="@color/cl_font_ea" android:id="@+id/line_root_base" android:layout_height="0.5dp" android:layout_width="match_parent"> </View></LinearLayout>
main.xml:
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto"> <include layout="@layout/layout_top"> </include></LinearLayout>
上面的代碼用include的layout屬性復用了topbar布局。include還可以指定layout_width、layout_height以及id。需要注意的是,如果指定了id屬性,被包含布局原本的root id將會失效,並被此id覆蓋。
2> viewstub
viewstub標籤與include很像,都通過layout屬性加載布局。不同之處在於:include用來復用布局,該布局會在App啟動加載時就被載入內存;而viewstub所引用的布局默認並不會被加載,只有當你在某個時刻真正需要時才會載入,從而在解析layout時節省CPU加載時間與內存佔用。
用法也很簡單:
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto"> <ViewStub android:id="@+id/vstub_network_error" android:layout="@layout/layout_network_error" android:layout_height="match_parent" android:layout_width="match_parent"> </ViewStub></LinearLayout>
在代碼中,我們通過findViewById獲取ViewStub,再調用其inflate方法即可拿到layout的View:
private void showEmptyView() { if (ValidateUtils.isNullObj(viewNetWorkError)) { ViewStub mViewStub = (ViewStub) findViewById(R.id.vstub_network_error); viewNetWorkError = mViewStub.inflate(); } else { viewNetWorkError.setVisibility(View.VISIBLE); } }
private void showCourseContent(Course course) { if (ValidateUtils.isNotNullObj(viewNetWorkError)) { viewNetWorkError.setVisibility(View.GONE); } }
ViewStub的適用場景非常廣泛:加載失敗界面、進度布局、信息出錯提示等等。
3> merge
merge標籤用來削減layout的層級。大家都知道,Android頂級布局DecorView的根布局是FrameLayout,如果我們在自己的布局裡再套一層FrameLayout,就會造成布局節點層級疊加、重複繪制。Android提供了一個名為Hierarchy Viewer的工具(位於sdk/tools目錄下)來查看布局節點資訊。
需要注意的是,merge標籤必須作為布局的根標籤,即不能嵌套在其他布局之中。用法很簡單,這裡就不貼代碼了。
介紹完這三個抽象標籤,想必大家已經知道如何在自己的布局中運用它們來做優化。此外,還有幾點同樣值得關注:
(1)inflate:inflate用於在代碼中加載現有布局,無論是LayoutInflater.from().inflate()還是Activity中的getLayoutInflater().inflate(),每次加載都有時間和內存消耗。因此要善用View復用來減少不必要的重複inflate。比如在ViewStub的場景中,可以把inflate出來的View存為全局變量,下次只需判斷是否為null即可復用,而不必反覆加載。
(2)ListView Adapter的getView方法:該方法中通常都會inflate一個布局再設置數據並返回。ListView在每個Item渲染時都會重複調用getView,而convertView是可以重複利用的,因此可以用ViewHolder模式復用convertView,避免每次都inflate。
(3)使用SurfaceView和TextureView:它們不會佔用UI Thread來加載渲染視圖,而是各自開啟線程異步加載,因此可用於優化加載速度與顯示效果。
(4)盡量為所有解析度創建資源:這樣能減少不必要的硬件縮放,從而提升UI繪製速度、降低內存消耗。
注意:網上有說法稱把View的可見性設為gone後,該View同樣不會被載入內存。這種說法其實是錯誤的。從Android源碼可知,即便把View的visibility設為gone,它仍然會被載入內存資源;只有通過ViewStub加載的layout,才不會在App啟動後被載入內存。
關於布局優化的內容就介紹到這裡,更多技巧還需要大家在实践中摸索。下一篇將與大家分享Android性能優化之數據優化。
Android 事件處理拾遺:觸摸、Configuration 與 AsyncTask
本節重點在 Android 事件處理機制中,除了常見的點擊與手勢,還有幾個實務上經常使用的主題,包括觸摸事件的兩種形式、Configuration 類別,以及 Async
Android Studio 系列教程(一):從下載安裝到第一個 HelloWorld
背景 Android Studio 是 Google 在 2013 年 I/O 大會上為 Android 開發推出的新一代 IDE,基於 IntelliJ IDEA 改造
淺析 Android 之 Adapter 用法總結
1. 概念 Adapter 是連接後端資料與前端顯示的適配器接口,是資料和 UI(View)之間一個重要的紐帶。在常見的 View(ListView、Grid
SEAndroid 如何保護 Android 系統屬性:從存儲結構到權限校驗
Android 系統通過「屬性」對外暴露設備與運行時信息,同時也可以通過設置屬性來控制系統行為。既然屬性能左右系統行為,它就必須像文件一樣被當作需要保護的資源對待。 在啟