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