Android SDK工具:使用layoutopt進行佈局優化
優化是需要一定技巧的,性能良好的代碼固然重要,但寫出優秀代碼的成本往往也很高。幸運的是,在Android SDK中有一個工具可以幫助你優化佈局,以減少內存消耗,提高應用程序運行性能。
【51CTO譯文】創建好看的Android佈局是個不小的挑戰,當你花了數小時調整好它們適應多種設備後,你通常不想再重新調整,但笨重的嵌套佈局效率往往非常低下,幸運的是,在Android SDK中有一個工具可以幫助你優化佈局,以減少內存消耗,提高應用程序運行性能。
優化是需要一定技巧的,性能良好的代碼固然重要,但寫出優秀代碼的成本往往也很高,你可能不會過早地貿然爲那些只運行一次或臨時功能代碼實施優化,如果你的應用程序反應遲鈍,並且賣得很貴,或使系統中的其它應用程序變慢,用戶一定會有所響應,你的應用程序下載量將很可能受到影響。
在開發期間儘早優化你的佈局是節省成本,提高性能的簡單方法,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 原文