Android佈局


本文是從網絡上覆制整理,方便個人閱讀。正確性不置可否。


Android佈局原則:

  1. 儘量多使用LinearLayout和RelativeLayout;FrameLayout使用在佈局疊加的時;AbsoluteLayout已經廢棄,不要使用;TableLayout已經被GridView替代,不建議使用。

  2. 在佈局層次一樣的情況下,建議使用LinearLayout代替RelativeLayout,因爲LinearLayout性能要稍高一點。

  3. 將可複用的標籤抽取出來並且通過include標籤使用。

  4. 使用merge標籤減少佈局的嵌套層次。

  5. 使用ViewStub標籤加載一些不常用的佈局。


       RelativeLayout和LinearLayout是Android中常用的佈局,兩者的使用會極大的影響程序生成每一幀的性能,因此,正確的使用它們是提升程序性能的重要工作。下面將通過分析它們的源碼來探討其View繪製性能,並得出其正確的使用方法。

RelativeLayout和LinearLayout是如何進行measure的?

        通過官方文檔我們知道View的繪製進行measure, layout, draw,分別對應onMeasure(), onLayout, onDraw(),而他們的性能差異主要在onMeasure()上。

首先是RelativeLayout:

 1 @Override
 2 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 3 ......
 4 View[] views = mSortedHorizontalChildren; 
 5 int count = views.length; 
 6  
 7 for (int i = 0; i < count; i++) { 
 8     View child = views[i]; 
 9     if (child.getVisibility() != GONE) {
 10         LayoutParams params = (LayoutParams) child.getLayoutParams();
 11         int[] rules = params.getRules(layoutDirection);
 12 
 13         applyHorizontalSizeRules(params, myWidth, rules);
 14         measureChildHorizontal(child, params, myWidth, myHeight);
 15 
 16         if (positionChildHorizontal(child, params, myWidth, isWrapContentWidth)) {
 17             offsetHorizontalAxis = true;
 18         }
 19     }
 20 }
 21 
 22 views = mSortedVerticalChildren;
 23 count = views.length;
 24 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
 25 
 26 for (int i = 0; i < count; i++) {
 27     View child = views[i];
 28     if (child.getVisibility() != GONE) {
 29         LayoutParams params = (LayoutParams) child.getLayoutParams();
 30         
 31         applyVerticalSizeRules(params, myHeight);
 32         measureChild(child, params, myWidth, myHeight);
 33         if (positionChildVertical(child, params, myHeight, isWrapContentHeight)) {
 34             offsetVerticalAxis = true;
 35         }
 36 
 37         if (isWrapContentWidth) {
 38             if (isLayoutRtl()) {
 39                 if (targetSdkVersion < Build.VERSION_CODES.KITKAT) {
 40                     width = Math.max(width, myWidth - params.mLeft);
 41                 } else {
 42                     width = Math.max(width, myWidth - params.mLeft - params.leftMargin);
 43                 }
 44             } else {
 45                 if (targetSdkVersion < Build.VERSION_CODES.KITKAT) {
 46                     width = Math.max(width, params.mRight);
 47                 } else {
 48                     width = Math.max(width, params.mRight + params.rightMargin);
 49                 }
 50             }
 51         }
 52 
 53         if (isWrapContentHeight) {
 54             if (targetSdkVersion < Build.VERSION_CODES.KITKAT) {
 55                 height = Math.max(height, params.mBottom);
 56             } else {
 57                 height = Math.max(height, params.mBottom + params.bottomMargin);
 58             }
 59         }
 60 
 61         if (child != ignore || verticalGravity) {
 62             left = Math.min(left, params.mLeft - params.leftMargin);
 63             top = Math.min(top, params.mTop - params.topMargin);
 64         }
 65 
 66         if (child != ignore || horizontalGravity) {
 67             right = Math.max(right, params.mRight + params.rightMargin);
 68             bottom = Math.max(bottom, params.mBottom + params.bottomMargin);
 69         }
 70     }
 71 }
 72 ......
 73 }

        

         根據上述關鍵代碼,RelativeLayout分別對所有子View進行兩次measure,橫向縱向分別進行一次。

     

  LinearLayout:     

1 @Override
2 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
3     if (mOrientation == VERTICAL) {
4         measureVertical(widthMeasureSpec, heightMeasureSpec);
5     } else {
6         measureHorizontal(widthMeasureSpec, heightMeasureSpec);
7     }
8 }

           

        根據線性佈局方向,執行不同的方法,這裏分析measureVertical方法。

       

 1 void measureVertical(int widthMeasureSpec, int heightMeasureSpec) { 
 2 ...... 
 3 for (int i = 0; i < count; ++i) { 
 4     ...... 
 5  
 6     LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams(); 
 7  
 8     totalWeight += lp.weight; 
 9     
 10     if (heightMode == MeasureSpec.EXACTLY && lp.height == 0 && lp.weight > 0) {
 11         // Optimization: don't bother measuring children who are going to use
 12         // leftover space. These views will get measured again down below if
 13         // there is any leftover space.
 14         final int totalLength = mTotalLength;
 15         mTotalLength = Math.max(totalLength, totalLength + lp.topMargin + lp.bottomMargin);
 16         skippedMeasure = true;
 17     } else {
 18         int oldHeight = Integer.MIN_VALUE;
 19 
 20         if (lp.height == 0 && lp.weight > 0) {
 21             // heightMode is either UNSPECIFIED or AT_MOST, and this
 22             // child wanted to stretch to fill available space.
 23             // Translate that to WRAP_CONTENT so that it does not end up
 24             // with a height of 0
 25             oldHeight = 0;
 26             lp.height = LayoutParams.WRAP_CONTENT;
 27         }
 28 
 29         // Determine how big this child would like to be. If this or
 30         // previous children have given a weight, then we allow it to
 31         // use all available space (and we will shrink things later
 32         // if needed).
 33         measureChildBeforeLayout(
 34                child, i, widthMeasureSpec, 0, heightMeasureSpec,
 35                totalWeight == 0 ? mTotalLength : 0);
 36 
 37         if (oldHeight != Integer.MIN_VALUE) {
 38            lp.height = oldHeight;
 39         }
 40 
 41         final int childHeight = child.getMeasuredHeight();
 42         final int totalLength = mTotalLength;
 43         mTotalLength = Math.max(totalLength, totalLength + childHeight + lp.topMargin +
 44                lp.bottomMargin + getNextLocationOffset(child));
 45 
 46         if (useLargestChild) {
 47             largestChildHeight = Math.max(childHeight, largestChildHeight);
 48         }
 49     }
 50     ......

           

        LinearLayout首先會對所有的子View進行measure,並計算totalWeight(所有子View的weight屬性之和),然後判斷子View的weight屬性是否爲最大,如爲最大則將剩餘的空間分配給它。如果不使用weight屬性進行佈局,則不進行第二次measure。

       

 1 // Either expand children with weight to take up available space or 
 2 // shrink them if they extend beyond our current bounds. If we skipped 
 3 // measurement on any children, we need to measure them now.
 4 int delta = heightSize - mTotalLength; 
 5 if (skippedMeasure || delta != 0 && totalWeight > 0.0f) { 
 6     float weightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight; 
 7  
 8     mTotalLength = 0; 
 9 
 10     for (int i = 0; i < count; ++i) {
 11         final View child = getVirtualChildAt(i);
 12         
 13         if (child.getVisibility() == View.GONE) {
 14             continue;
 15         }
 16         
 17         LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();
 18         
 19         float childExtra = lp.weight;
 20         if (childExtra > 0) {
 21             // Child said it could absorb extra space -- give him his share
 22             int share = (int) (childExtra * delta / weightSum);
 23             weightSum -= childExtra;
 24             delta -= share;
 25 
 26             final int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
 27                     mPaddingLeft + mPaddingRight +
 28                             lp.leftMargin + lp.rightMargin, lp.width);
 29 
 30             // TODO: Use a field like lp.isMeasured to figure out if this
 31             // child has been previously measured
 32             if ((lp.height != 0) || (heightMode != MeasureSpec.EXACTLY)) {
 33                 // child was measured once already above...
 34                 // base new measurement on stored values
 35                 int childHeight = child.getMeasuredHeight() + share;
 36                 if (childHeight < 0) {
 37                     childHeight = 0;
 38                 }
 39                 
 40                 child.measure(childWidthMeasureSpec,
 41                         MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY));
 42             } else {
 43                 // child was skipped in the loop above.
 44                 // Measure for this first time here  
 45                 child.measure(childWidthMeasureSpec,
 46                         MeasureSpec.makeMeasureSpec(share > 0 ? share : 0,
 47                                 MeasureSpec.EXACTLY));
 48             }
 49 
 50             // Child may now not fit in vertical dimension.
 51             childState = combineMeasuredStates(childState, child.getMeasuredState()
 52                     & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
 53         }
 54 
 55         ......
 56     }
 57      ......
 58 } else {
 59     alternativeMaxWidth = Math.max(alternativeMaxWidth,
 60                                    weightedMaxWidth);
 61 
 62 
 63     // We have no limit, so make all weighted views as tall as the largest child.
 64     // Children will have already been measured once.     
 65     if (useLargestChild && heightMode != MeasureSpec.EXACTLY) {
 66         for (int i = 0; i < count; i++) {
 67             final View child = getVirtualChildAt(i);
 68 
 69             if (child == null || child.getVisibility() == View.GONE) {
 70                 continue;
 71             }
 72 
 73             final LinearLayout.LayoutParams lp =
 74                     (LinearLayout.LayoutParams) child.getLayoutParams();
 75 
 76             float childExtra = lp.weight;
 77             if (childExtra > 0) {
 78                 child.measure(
 79                         MeasureSpec.makeMeasureSpec(child.getMeasuredWidth(),
 80                                 MeasureSpec.EXACTLY),
 81                         MeasureSpec.makeMeasureSpec(largestChildHeight,
 82                                 MeasureSpec.EXACTLY));
 83             }
 84         }
 85     }
 86 }
 87 ......
 88 }

         

提高繪製性能的使用方式

        根據上面源碼的分析,RelativeLayout將對所有的子View進行兩次measure,而LinearLayout在使用 weight屬性進行佈局時也會對子View進行兩次measure,如果他們位於整個View樹的頂端時並可能進行多層的嵌套時,位於底層的View將 會進行大量的measure操作,大大降低程序性能。因此,應儘量將RelativeLayout和LinearLayout置於View樹的底層,並減 少嵌套。



        二、Measure 和 Layout

        從整體上來看 Measure 和 Layout 兩個步驟的執行: MeasureLayout
        樹的遍歷是有序的,由父視圖到子視圖,每一個 ViewGroup 負責測繪它所有的子視圖,而最底層的 View 會負責測繪自身。


        具體分析


        measure 過程由measure(int, int)方法發起,從上到下有序的測量 View ,在 measure 過程的最後,每個視圖存儲了自己的尺寸大小和測量規格。 layout 過程由layout(int, int, int, int)方法發起,也是自上而下進行遍歷。在該過程中,每個父視圖會根據 measure 過程得到的尺寸來擺放自己的子視圖。

        measure 過程會爲一個View及所有子節點的 mMeasuredWidth 和 mMeasuredHeight 變量賦值,該值可以通過 getMeasuredWidth()getMeasuredHeight()方 法獲得。而且這兩個值必須在父視圖約束範圍之內,這樣纔可以保證所有的父視圖都接收所有子視圖的測量。如果子視圖對於 Measure 得到的大小不滿意的時候,父視圖會介入並設置測量規則進行第二次 measure。比如,父視圖可以先根據未給定的 dimension 去測量每一個子視圖,如果最終子視圖的未約束尺寸太大或者太小的時候,父視圖就會使用一個確切的大小再次對子視圖進行 measure 。

        measure 過程傳遞尺寸的兩個類

  • ViewGroup.LayoutParams (View 自身的佈局參數)

  • MeasureSpecs 類(父視圖對子視圖的測量要求)

    ViewGroup.LayoutParams
    這個類我們很常見,就是用來指定視圖的高度和寬度等參數。對於每個視圖的 height 和 width,你有以下選擇:

    • MATCH_PARENT 表示子視圖希望和父視圖一樣大(不包含padding值)

    • WRAP_CONTENT 表示視圖爲正好能包裹其內容大小(包含padding值)

ViewGroup 的子類有其對應的 ViewGroup.LayoutParams 的子類。比如 RelativeLayout 擁有的 ViewGroup.LayoutParams 的子類 RelativeLayoutParams。
有時我們需要使用 view.getLayoutParams() 方法獲取一個視圖 LayoutParams ,然後進行強轉,但由於不知道其具體類型,可能會導致強轉錯誤。其實該方法得到的就是其所在父視圖類型的 LayoutParams,比如 View 的父控件爲 RelativeLayout,那麼得到的 LayoutParams 類型就爲 RelativeLayoutParams。

MeasureSpecs
測量規格,包含測量要求和尺寸的信息,有三種模式:

  • UNSPECIFIED
    父視圖不對子視圖有任何約束,它可以達到所期望的任意尺寸。比如ListView、ScrollView,一般自定義View中用不到,

  • EXACTLY
    父視圖爲子視圖指定一個確切的尺寸,而且無論子視圖期望多大,它都必須在該指定大小的邊界內,對應的屬性爲 match_parent 或具體指,比如 100dp,父控件可以通過MeasureSpec.getSize(measureSpec)直接得到子控件的尺寸。

  • AT_MOST
    父視圖爲子視圖指定一個最大尺寸。子視圖必須確保它自己所有子視圖可以適應在該尺寸範圍內,對應的屬性爲 wrap_content,這種模式下,父控件無法確定子 View 的尺寸,只能由子控件自己根據需求去計算自己的尺寸,這種模式就是我們自定義視圖需要實現測量邏輯的情況。


       三、include

        在實際開發中,我們經常會遇到一些共用的UI組件,比如帶返回按鈕的導航欄,如果爲每一個xml文件都設置這部分佈局,一是重複的工作量大,二是如果有變更,那麼每一個xml文件都得修改。不過,我們可以將這些共用的組件抽取出來單獨放到一個xml文件中,然後使用< include />標籤導入到相應佈局


        四、merge

        < merge />標籤的作用是合併UI佈局,使用該標籤能降低UI佈局的嵌套層次。該標籤的主要使用場景主要包括兩個,第一種情況是當xml文件的根佈局是FrameLayout時,可以用merge作爲根節點。理由是因爲Activity的內容佈局中,默認就用了一個FrameLayout作爲xml佈局根節點的父節點;第二種情況是當用include標籤導入一個共用佈局時,如果父佈局和子佈局根節點爲同一類型,可以使用merge將子節點佈局的內容合併包含到父佈局中,這樣就可以減少一級嵌套層次。這樣就降低了佈局嵌套層次。


        ViewStub

        ViewStub是Android佈局優化中一個很不錯的標籤/控件,直接繼承自View。但是真正用的可能不多。當對一個ViewStub調用inflate()方法或設置它可見時,系統會加載在ViewStub標籤中引入的我們自己定義的View,然後填充在父布 局當中。也就是說,在對ViewStub調用inflate()方法或設置visible之前,它是不佔用佈局空間和系統資源的。它的使用場景可以是在我 們需要加載並顯示一些不常用的View時,例如一些網絡異常的提示信息等。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章