Android工具layoutopt

Layoutopt工具是一款簡單的命令行工具,它能夠幫助你找到沒必要要的控件嵌套以及縮減佈局資源的其餘方法,以便儘可能減小資源的使用。它讓你能夠了解哪些佈局控件多是多餘的或沒必要要的。控件越少、佈局層次越淺,性能就越好。java

運行layoutopt工具是至關簡單的,只須要跟上一個佈局文件或佈局文件所在目錄做爲參數,須要注意的是,這裏你必須包括佈局文件或目錄的完整路徑,即便你當前就位於這個目錄。咱們來看一個簡單的例子:android

D:\d\tools\eclipse\article_ws\Nothing\res\layout>layoutopt main.xml

Layoutopt的輸出結果只是建議,你能夠有選擇地在你的應用程序中採納這些建議,下面來看幾個使用layoutopt輸出建議的例子。less

1. 無用的佈局eclipse

在佈局設計期間,咱們會頻繁地移動各類組件,有些組件最終可能會再也不使用,如:工具

<?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

輸出結果每一行最前面的兩個數字表示建議的行號。性能

2.根能夠替換測試

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>

這個佈局將返回下面的輸出:spa

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不能提供的屬性。

3.太多的視圖

每一個視圖都會消耗內存,在一個佈局中佈置太多的視圖,佈局會佔用過多的內存,假設一個佈局包含超過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,固然最新的設備有可能可以支持這麼多視圖,但若是真的出現性能不佳的狀況,最好採納這個建議。

4.嵌套太多

佈局不該該有太多的嵌套,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給出的優化建議,雖然採納建議做出修改不會當即大幅改善性能,但沒有理由須要複雜的佈局拖慢整個應用程序的速度,而且後期的維護難度也很大。簡單佈局不只簡化了開發週期,還能夠減小測試和維護工做量,所以,在應用程序開發期間,應儘早優化你的佈局,不要等到最後用戶反饋回來再作修改。

相關文章
相關標籤/搜索