BKJIA譯文】建立好看的Android布局是個不小的挑戰,當你花了數小時調整好它們適應多種裝置後,你通常不想再重新調整,但笨重的嵌套布局效率往往非常低下,幸運的是,在Android SDK中有一個工具可以協助你最佳化布局,以減少記憶體消耗,提高應用程式運行效能。
layoutoptimization
最佳化是需要一定技巧的,效能良好的代碼固然重要,但寫出優秀代碼的成本往往也很高,你可能不會過早地貿然為那些只運行一次或臨時功能代碼實施最佳化,如果你的應用程式反應遲鈍,並且賣得很貴,或使系統中的其它應用程式變慢,使用者一定會有所響應,你的應用程式下載量將很可能受到影響。
在開發期間儘早最佳化你的布局是節省成本,提高效能的簡單方法,Android SDK帶來了一個工具,它可以自動分析你的布局,發現可能並不需要的布局元素,以降低布局複雜度。
第一步:準備工作
如果想使用Android SDK中提供的最佳化工具,你需要在開發系統的命令列中工作,如果你不熟悉使用命令列工具,那麼你得多下功夫學習了。
我們強烈建議你將Android工具所在的路徑添加到作業系統的環境變數中,這樣就可以直接敲名字運行相關的工具了,否則每次都要在命令提示字元後面輸入完整的檔案路徑,現在在Android SDK中有兩個工具目錄:/tools和/platform-tools,本文主要使用位於/tools目錄中的layoutopt工具,另外我想說的是,ADB工具位於/platform-tools目錄下。
運行layoutopt
運行layoutopt工具是相當簡單的,只需要跟上一個布局檔案或布局檔案所在目錄作為參數,需要注意的是,這裡你必須包括布局檔案或目錄的完整路徑,即使你當前就位於這個目錄。我們來看一個簡單的例子:
- 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>
注意,在上面的樣本中,包含了檔案的完整路徑,如果不指定完整路徑,不會輸出任何內容,例如:
- D:\d\tools\eclipse\article_ws\Nothing\res\layout>layoutopt main.xml
- D:\d\tools\eclipse\article_ws\Nothing\res\layout>
因此,如果你看不任何東西,則很可能是檔案未被解析,也就是說檔案可能未被找到。
使用layoutopt輸出
Layoutopt的輸出結果只是建議,你可以有選擇地在你的應用程式中採納這些建議,下面來看幾個使用layoutopt輸出建議的例子。
無用的布局
在布局設計期間,我們會頻繁地移動各種組件,有些組件最終可能會不再使用,如:
- <?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內的LinearLayout是多餘的:
- 11:17 This LinearLayout layout or its LinearLayout parent is useless
輸出結果每一行最前面的兩個數字表示建議的行號。
根可以替換
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應該保留,而第二行的建議則可以採納,可以刪除無用的FrameLayout。
有趣的是,這個工具不是全能的,例如,在上面的例子中,如果我們給FrameLayout添加一個背景屬性,然後再運行這個工具,第一個建議當然會消失,但第二個建議仍然會顯示,工具知道我們不能通過合并控制背景,但檢查了LinearLayout後,它似乎就忘了我們還給FrameLayout添加了一個LinearLayout不能提供的屬性。
太多的視圖
每個視圖都會消耗記憶體,在一個布局中布置太多的視圖,布局會佔用過多的記憶體,假設一個布局包含超過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!
上面給出的建議是視圖數量不能超過80,當然最新的裝置有可能能夠支援這麼多視圖,但如果真的出現效能不佳的情況,最好採納這個建議。
嵌套太多
布局不應該有太多的嵌套,layoutopt和Android團隊)建議布局保持在10級以內,即使是最大的平板電腦螢幕,布局也不應該超過10級,RelativeLayout可能是一個解決辦法,但它的用法更複雜,好在Eclipse中的Graphical Layout資源工具更新後,使得這一切變得更簡單。
下面是布局嵌套太多時,layoutopt的輸出內容:
- -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
嵌套布局警告通常伴隨有一些無用布局的警告,有助於找出哪些布局可以移除,避免螢幕布局全部重新設計。
小結
Layoutopt是一個快速易用的布局分析工具,找出低效和無用的布局,你要做的是判斷是否採納layoutopt給出的最佳化建議,雖然採納建議作出修改不會立即大幅改善效能,但沒有理由需要複雜的布局拖慢整個應用程式的速度,並且後期的維護難度也很大。簡單布局不僅簡化了開發週期,還可以減少測試和維護工作量,因此,在應用程式開發期間,應儘早最佳化你的布局,不要等到最後使用者反饋回來再做修改。
原文名:Android SDK Tools: Layout Optimization 作者:Shane Conder和Lauren Darcey 原文
BKJIA譯稿,非經授權謝絕轉載,合作媒體轉載請註明原文出處、作者及BKJIA譯稿和譯者!】