前言
布局優(yōu)化對(duì)于每個(gè)項(xiàng)目總是必不可少弟劲,本文主要介紹使用抽象布局標(biāo)簽(include, viewstub, merge)饭弓、去除不必要的嵌套和View節(jié)點(diǎn)诗祸、減少不必要的infalte及其他Layout方面可調(diào)優(yōu)點(diǎn)翩肌,順帶提及布局調(diào)優(yōu)相關(guān)工具(hierarchy viewer和lint)蹲诀。
抽象布局標(biāo)簽
< include >標(biāo)簽
include標(biāo)簽常用于將布局中的公共部分提取出來供其他layout共用迂苛,以實(shí)現(xiàn)布局模塊化三热,這在布局編寫方便提供了大大的便利。
下面以在一個(gè)布局main.xml中用include引入另一個(gè)布局foot.xml為例三幻。main.mxl代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/simple_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/dp_80" />
<include layout="@layout/foot.xml" />
</RelativeLayout>
其中include引入的foot.xml為公用的頁面底部康铭,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_40"
android:layout_above="@+id/text"/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_40"
android:layout_alignParentBottom="true"
android:text="@string/app_name" />
</RelativeLayout>
< include >標(biāo)簽唯一需要的屬性是layout屬性,指定需要包含的布局文件赌髓〈犹伲可以定義android:id和android:layout_*屬性來覆蓋被引入布局根節(jié)點(diǎn)的對(duì)應(yīng)屬性值。注意重新定義android:id后锁蠕,子布局的頂結(jié)點(diǎn)i就變化了夷野。
< viewstub >標(biāo)簽
viewstub標(biāo)簽同include標(biāo)簽一樣可以用來引入一個(gè)外部布局,不同的是荣倾,viewstub引入的布局默認(rèn)不會(huì)擴(kuò)張悯搔,即既不會(huì)占用顯示也不會(huì)占用位置,從而在解析layout時(shí)節(jié)省cpu和內(nèi)存舌仍。
viewstub常用來引入那些默認(rèn)不會(huì)顯示妒貌,只在特殊情況下顯示的布局通危,如進(jìn)度布局、網(wǎng)絡(luò)失敗顯示的刷新布局灌曙、信息出錯(cuò)出現(xiàn)的提示布局等后裸。
下面以在一個(gè)布局main.xml中加入網(wǎng)絡(luò)錯(cuò)誤時(shí)的提示頁面network_error.xml為例仅仆。main.mxl代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
……
<ViewStub
android:id="@+id/network_error_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout="@layout/network_error" />
</RelativeLayout>
其中network_error.xml為只有在網(wǎng)絡(luò)錯(cuò)誤時(shí)才需要顯示的布局,默認(rèn)不會(huì)被解析,示例代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/network_setting"
android:layout_width="@dimen/dp_160"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/network_setting" />
<Button
android:id="@+id/network_refresh"
android:layout_width="@dimen/dp_160"
android:layout_height="wrap_content"
android:layout_below="@+id/network_setting"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dp_10"
android:text="@string/network_refresh" />
</RelativeLayout>
在java中通過(ViewStub)findViewById(id)找到ViewStub凶杖,通過stub.inflate()展開ViewStub疆液,然后得到子View偿警,如下:
private View networkErrorView;
private void showNetError() {
// not repeated infalte
if (networkErrorView != null) {
networkErrorView.setVisibility(View.VISIBLE);
return;
}
ViewStub stub = (ViewStub)findViewById(R.id.network_error_layout);
networkErrorView = stub.inflate();
Button networkSetting = (Button)networkErrorView.findViewById(R.id.network_setting);
Button refresh = (Button)findViewById(R.id.network_refresh);
}
private void showNormal() {
if (networkErrorView != null) {
networkErrorView.setVisibility(View.GONE);
}
}
在上面showNetError()中展開了ViewStub负间,同時(shí)我們對(duì)networkErrorView進(jìn)行了保存,這樣下次不用繼續(xù)inflate颖杏。這就是后面第三部分提到的減少不必要的infalte纯陨。
viewstub標(biāo)簽大部分屬性同include標(biāo)簽類似。
上面展開ViewStub部分代碼
ViewStub stub = (ViewStub)findViewById(R.id.network_error_layout);
networkErrorView = stub.inflate();
也可以寫成下面的形式
View viewStub = findViewById(R.id.network_error_layout);
viewStub.setVisibility(View.VISIBLE); // ViewStub被展開后的布局所替換
networkErrorView = findViewById(R.id.network_error_layout); // 獲取展開后的布局
效果一致留储,只是不用顯示的轉(zhuǎn)換為ViewStub队丝。通過viewstub的原理我們可以知道將一個(gè)view設(shè)置為GONE不會(huì)被解析,從而提高layout解析速度欲鹏,而VISIBLE和INVISIBLE這兩個(gè)可見性屬性會(huì)被正常解析。
< merge >標(biāo)簽
在使用了include后可能導(dǎo)致布局嵌套過多臭墨,多余不必要的layout節(jié)點(diǎn)赔嚎,從而導(dǎo)致解析變慢,不必要的節(jié)點(diǎn)和嵌套可通過hierarchy viewer(下面布局調(diào)優(yōu)工具中有具體介紹)或設(shè)置->開發(fā)者選項(xiàng)->顯示布局邊界查看胧弛。
merge標(biāo)簽可用于兩種典型情況:
- 布局頂結(jié)點(diǎn)是FrameLayout且不需要設(shè)置background或padding等屬性尤误,可以用merge代替,因?yàn)锳ctivity內(nèi)容試圖的parent view就是個(gè)FrameLayout结缚,所以可以用merge消除只剩一個(gè)损晤。
- 某布局作為子布局被其他布局include時(shí),使用merge當(dāng)作該布局的頂節(jié)點(diǎn)红竭,這樣在被引入時(shí)頂結(jié)點(diǎn)會(huì)自動(dòng)被忽略尤勋,而將其子節(jié)點(diǎn)全部合并到主布局中。
以< include >標(biāo)簽的示例為例茵宪,用hierarchy viewer查看main.xml布局如下圖:
可以發(fā)現(xiàn)多了一層沒必要的RelativeLayout最冰,將foot.xml中RelativeLayout改為merge,如下:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_40"
android:layout_above="@+id/text"/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_40"
android:layout_alignParentBottom="true"
android:text="@string/app_name" />
</merge>
運(yùn)行后再次用hierarchy viewer查看main.xml布局如下圖:
這樣就不會(huì)有多余的RelativeLayout節(jié)點(diǎn)了稀火。
優(yōu)化
去除不必要的嵌套和View節(jié)點(diǎn)
- 首次不需要使用的節(jié)點(diǎn)設(shè)置為GONE或使用viewstub
- 使用RelativeLayout代替LinearLayout
大約在Android4.0之前暖哨,新建工程的默認(rèn)main.xml中頂節(jié)點(diǎn)是LinearLayout,而在之后已經(jīng)改為RelativeLayout凰狞,因?yàn)镽elativeLayout性能更優(yōu)篇裁,且可以簡(jiǎn)單實(shí)現(xiàn)LinearLayout嵌套才能實(shí)現(xiàn)的布局沛慢。
4.0及以上Android版本可通過設(shè)置->開發(fā)者選項(xiàng)->顯示布局邊界打開頁面布局顯示,看看是否有不必要的節(jié)點(diǎn)和嵌套达布。4.0以下版本可通過hierarchy viewer查看团甲。
減少不必要的infalte
對(duì)于inflate的布局可以直接緩存,用全部變量代替局部變量往枣,避免下次需再次inflate
如上面ViewStub示例中的
if (networkErrorView != null) {
networkErrorView.setVisibility(View.VISIBLE);
return;
}
其他優(yōu)化點(diǎn)
用SurfaceView或TextureView代替普通View
SurfaceView或TextureView可以通過將繪圖操作移動(dòng)到另一個(gè)單獨(dú)線程上提高性能伐庭。
普通View的繪制過程都是在主線程(UI線程)中完成,如果某些繪圖操作影響性能就不好優(yōu)化了分冈,這時(shí)我們可以考慮使用SurfaceView和TextureView圾另,他們的繪圖操作發(fā)生在UI線程之外的另一個(gè)線程上。
因?yàn)镾urfaceView在常規(guī)視圖系統(tǒng)之外雕沉,所以無法像常規(guī)試圖一樣移動(dòng)集乔、縮放或旋轉(zhuǎn)一個(gè)SurfaceView。TextureView是Android4.0引入的坡椒,除了與SurfaceView一樣在單獨(dú)線程繪制外扰路,還可以像常規(guī)視圖一樣被改變。
使用RenderJavascript
RenderScript是Adnroid3.0引進(jìn)的用來在Android上寫高性能代碼的一種語言倔叼,語法給予C語言的C99標(biāo)準(zhǔn)汗唱,他的結(jié)構(gòu)是獨(dú)立的,所以不需要為不同的CPU或者GPU定制代碼代碼丈攒。
使用OpenGL繪圖
Android支持使用OpenGL API的高性能繪圖哩罪,這是Android可用的最高級(jí)的繪圖機(jī)制,在游戲類對(duì)性能要求較高的應(yīng)用中得到廣泛使用巡验。
Android 4.3最大的改變际插,就是支持OpenGL ES 3.0。相比2.0显设,3.0有更多的緩沖區(qū)對(duì)象框弛、增加了新的著色語言、增加多紋理支持等等捕捂,將為Android游戲帶來更出色的視覺體驗(yàn)瑟枫。
盡量為所有分辨率創(chuàng)建資源
減少不必要的硬件縮放,這會(huì)降低UI的繪制速度指攒,可借助Android asset studio
布局調(diào)優(yōu)工具
hierarchy viewer
hierarchy viewer可以方便的查看Activity的布局力奋,各個(gè)View的屬性、measure幽七、layout景殷、draw的時(shí)間,如果耗時(shí)較多會(huì)用紅色標(biāo)記,否則顯示綠色猿挚。
hierarchy viewer.bat位于<sdk>/tools/目錄下咐旧。使用可見:Using Hierarchy Viewer , 示例圖如下:
layoutopt
layoutopt是一個(gè)可以提供layout及其層級(jí)優(yōu)化提示的命令行,在sdk16以后已經(jīng)被lint取代绩蜻,在Windows->Show View->Other->Android->Lint Warnings查看lint優(yōu)化提示铣墨,lint具體介紹可見Improving Your Code with lint。
Layout Inspector
Android Studio 的面板上選擇 Tools -> Android -> Layout Inspector 即可
這個(gè)工具也十分強(qiáng)大办绝,可以直接對(duì)著你的視圖查看整個(gè)的層級(jí)
你也可以查看一個(gè)View的詳細(xì)的信息