Android SDK工具:使用layoutopt進(jìn)行布局優(yōu)化
原創(chuàng)【51CTO譯文】創(chuàng)建好看的Android布局是個不小的挑戰(zhàn),當(dāng)你花了數(shù)小時調(diào)整好它們適應(yīng)多種設(shè)備后,你通常不想再重新調(diào)整,但笨重的嵌套布局效率往往非常低下,幸運的是,在Android SDK中有一個工具可以幫助你優(yōu)化布局,以減少內(nèi)存消耗,提高應(yīng)用程序運行性能。
layoutoptimization
優(yōu)化是需要一定技巧的,性能良好的代碼固然重要,但寫出優(yōu)秀代碼的成本往往也很高,你可能不會過早地貿(mào)然為那些只運行一次或臨時功能代碼實施優(yōu)化,如果你的應(yīng)用程序反應(yīng)遲鈍,并且賣得很貴,或使系統(tǒng)中的其它應(yīng)用程序變慢,用戶一定會有所響應(yīng),你的應(yīng)用程序下載量將很可能受到影響。
在開發(fā)期間盡早優(yōu)化你的布局是節(jié)省成本,提高性能的簡單方法,Android SDK帶來了一個工具,它可以自動分析你的布局,發(fā)現(xiàn)可能并不需要的布局元素,以降低布局復(fù)雜度。
***步:準(zhǔn)備工作
如果想使用Android SDK中提供的優(yōu)化工具,你需要在開發(fā)系統(tǒng)的命令行中工作,如果你不熟悉使用命令行工具,那么你得多下功夫?qū)W習(xí)了。
我們強(qiáng)烈建議你將Android工具所在的路徑添加到操作系統(tǒng)的環(huán)境變量中,這樣就可以直接敲名字運行相關(guān)的工具了,否則每次都要在命令提示符后面輸入完整的文件路徑,現(xiàn)在在Android SDK中有兩個工具目錄:/tools和/platform-tools,本文主要使用位于/tools目錄中的layoutopt工具,另外我想說的是,ADB工具位于/platform-tools目錄下。
運行l(wèi)ayoutopt
運行l(wèi)ayoutopt工具是相當(dāng)簡單的,只需要跟上一個布局文件或布局文件所在目錄作為參數(shù),需要注意的是,這里你必須包括布局文件或目錄的完整路徑,即使你當(dāng)前就位于這個目錄。我們來看一個簡單的例子:
- D:\d\tools\eclipse\article_ws\Nothing\res\layout>layoutopt D:\d\tools\eclipse\article_ws\Nothing\res\layout\main.xml
- D:\d\tools\eclipse\article_ws\Nothing\res\layout\main.xml
- D:\d\tools\eclipse\article_ws\Nothing\res\layout>
注意,在上面的示例中,包含了文件的完整路徑,如果不指定完整路徑,不會輸出任何內(nèi)容,例如:
- D:\d\tools\eclipse\article_ws\Nothing\res\layout>layoutopt main.xml
- D:\d\tools\eclipse\article_ws\Nothing\res\layout>
因此,如果你看不任何東西,則很可能是文件未被解析,也就是說文件可能未被找到。
使用layoutopt輸出
Layoutopt的輸出結(jié)果只是建議,你可以有選擇地在你的應(yīng)用程序中采納這些建議,下面來看幾個使用layoutopt輸出建議的例子。
無用的布局
在布局設(shè)計期間,我們會頻繁地移動各種組件,有些組件最終可能會不再使用,如:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
- <LinearLayout
- android:id="@+id/linearLayout1"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:orientation="vertical">
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:text="TextView"
- android:layout_height="wrap_content"></TextView>
- </LinearLayout>
- </LinearLayout>
工具將會很快告訴我們LinearLayout內(nèi)的LinearLayout是多余的:
- 11:17 This LinearLayout layout or its LinearLayout parent is useless
輸出結(jié)果每一行最前面的兩個數(shù)字表示建議的行號。
根可以替換
Layoutopt的輸出有時是矛盾的,例如:
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:id="@+id/linearLayout1"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:orientation="vertical">
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:text="TextView"
- android:layout_height="wrap_content"></TextView>
- <TextView
- android:text="TextView"
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></TextView>
- </LinearLayout>
- </FrameLayout>
這個布局將返回下面的輸出:
- 5:22 The root-level <FrameLayout/> can be replaced with <merge/>
- 10:21 This LinearLayout layout or its FrameLayout parent is useless
***行的建議雖然可行,但不是必需的,我們希望兩個TextView垂直放置,因此LinearLayout應(yīng)該保留,而第二行的建議則可以采納,可以刪除無用的FrameLayout。
有趣的是,這個工具不是全能的,例如,在上面的例子中,如果我們給FrameLayout添加一個背景屬性,然后再運行這個工具,***個建議當(dāng)然會消失,但第二個建議仍然會顯示,工具知道我們不能通過合并控制背景,但檢查了LinearLayout后,它似乎就忘了我們還給FrameLayout添加了一個LinearLayout不能提供的屬性。
太多的視圖
每個視圖都會消耗內(nèi)存,在一個布局中布置太多的視圖,布局會占用過多的內(nèi)存,假設(shè)一個布局包含超過80個視圖,layoutopt可能會給出下面這樣的建議:
- -1:-1 This layout has too many views: 83 views, it should have <= 80!
- -1:-1 This layout has too many views: 82 views, it should have <= 80!
- -1:-1 This layout has too many views: 81 views, it should have <= 80!
上面給出的建議是視圖數(shù)量不能超過80,當(dāng)然***的設(shè)備有可能能夠支持這么多視圖,但如果真的出現(xiàn)性能不佳的情況,***采納這個建議。
嵌套太多
布局不應(yīng)該有太多的嵌套,layoutopt(和Android團(tuán)隊)建議布局保持在10級以內(nèi),即使是***的平板電腦屏幕,布局也不應(yīng)該超過10級,RelativeLayout可能是一個解決辦法,但它的用法更復(fù)雜,好在Eclipse中的Graphical Layout資源工具更新后,使得這一切變得更簡單。
下面是布局嵌套太多時,layoutopt的輸出內(nèi)容:
- -1:-1 This layout has too many nested layouts: 12 levels, it should have <= 10!
- 305:318 This LinearLayout layout or its RelativeLayout parent is possibly useless
- 307:314 This LinearLayout layout or its FrameLayout parent is possibly useless
- 310:312 This LinearLayout layout or its LinearLayout parent is possibly useless
嵌套布局警告通常伴隨有一些無用布局的警告,有助于找出哪些布局可以移除,避免屏幕布局全部重新設(shè)計。
小結(jié)
Layoutopt是一個快速易用的布局分析工具,找出低效和無用的布局,你要做的是判斷是否采納layoutopt給出的優(yōu)化建議,雖然采納建議作出修改不會立即大幅改善性能,但沒有理由需要復(fù)雜的布局拖慢整個應(yīng)用程序的速度,并且后期的維護(hù)難度也很大。簡單布局不僅簡化了開發(fā)周期,還可以減少測試和維護(hù)工作量,因此,在應(yīng)用程序開發(fā)期間,應(yīng)盡早優(yōu)化你的布局,不要等到***用戶反饋回來再做修改。
原文名:Android SDK Tools: Layout Optimization 作者:Shane Conder和Lauren Darcey 原文
【51CTO譯稿,非經(jīng)授權(quán)謝絕轉(zhuǎn)載,合作媒體轉(zhuǎn)載請注明原文出處、作者及51CTO譯稿和譯者!】
【編輯推薦】