自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android進階之深入理解View的布局(Layout)流程原理

移動開發(fā) Android
View的layout方法作用是確定View的位置,ViewGroup的layout方法不僅要確定自身的位置,還有確定子View的位置。

[[424470]]

前言

前一篇我們講解了View的Measure過程,那今天我們來講解下Layout;

View的layout方法作用是確定View的位置,ViewGroup的layout方法不僅要確定自身的位置,還有確定子View的位置;

Android進階之深入理解View的測量(Measure)流程機制

一、Layout流程源碼詳解

1、performLayout

View三大工作流程是從ViewRootImpl#performTraversals開始的,其中performMeasure、performLayout、performDraw方法分別對應了View的測量、布局、繪制;

從performLayout開始分析View布局流程;

  1. private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth, 
  2.             int desiredWindowHeight) { 
  3.         mLayoutRequested = false
  4.         mScrollMayChange = true
  5.         mInLayout = true
  6.         final View host = mView; 
  7.         Trace.traceBegin(Trace.TRACE_TAG_VIEW, "layout"); 
  8.         try { 
  9.             host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight()); 
  10.             //省略... 
  11.         } finally { 
  12.             Trace.traceEnd(Trace.TRACE_TAG_VIEW); 
  13.         } 
  14.         mInLayout = false
  15.     } 

方法中的mView其實就是DecorView,那么host也就代表了DecorView,DecorView其實是個FrameLayout,ViewGroup并沒有重寫layout方法,所以我們來看下View#layout方法

2、layout

  1. /** 
  2.   * 源碼分析起始點:layout() 
  3.   * 作用:確定View本身的位置,即設置View本身的四個頂點位置 
  4.   */  
  5.   public void layout(int l, int t, int r, int b) {   
  6.     // 當前視圖的四個頂點 
  7.     int oldL = mLeft;   
  8.     int oldT = mTop;   
  9.     int oldB = mBottom;   
  10.     int oldR = mRight;   
  11.     // 1. 確定View的位置:setFrame() / setOpticalFrame() 
  12.     // 即初始化四個頂點的值、判斷當前View大小和位置是否發(fā)生了變化 & 返回  
  13.     // setFrame() ->分析1 
  14.     // setOpticalFrame() ->分析2 
  15.     boolean changed = isLayoutModeOptical(mParent) ? setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b); 
  16.     // 2. 若視圖的大小 & 位置發(fā)生變化 
  17.     // 會重新確定該View所有的子View在父容器的位置:onLayout() 
  18.     if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {   
  19.       onLayout(changed, l, t, r, b);   
  20.       // 對于單一View的laytou過程:由于單一View是沒有子View的,故onLayout()是一個空實現 ->分析3 
  21.       // 對于ViewGroup的laytou過程:由于確定位置與具體布局有關,所以onLayout()在ViewGroup為1個抽象方法,需自定義重寫實現(下面的章節(jié)會詳細說明) 
  22. }   
  23. /** 
  24.   * 分析1:setFrame() 
  25.   * 作用:根據傳入的4個位置值,設置View本身的四個頂點位置 
  26.   * 即:最終確定View本身的位置 
  27.   */  
  28.   protected boolean setFrame(int leftint topint rightint bottom) { 
  29.     // 通過以下賦值語句記錄下了視圖的位置信息,即確定View的四個頂點 
  30.     // 從而確定了視圖的位置 
  31.     mLeft = left
  32.     mTop = top
  33.     mRight = right
  34.     mBottom = bottom; 
  35.     mRenderNode.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom); 
  36.    } 
  37. /** 
  38.   * 分析2:setOpticalFrame() 
  39.   * 作用:根據傳入的4個位置值,設置View本身的四個頂點位置 
  40.   * 即:最終確定View本身的位置 
  41.   */  
  42.   private boolean setOpticalFrame(int leftint topint rightint bottom) { 
  43.         Insets parentInsets = mParent instanceof View ? 
  44.                 ((View) mParent).getOpticalInsets() : Insets.NONE; 
  45.         Insets childInsets = getOpticalInsets(); 
  46.         // 內部實際上是調用setFrame() 
  47.         return setFrame( 
  48.                 left   + parentInsets.left - childInsets.left
  49.                 top    + parentInsets.top  - childInsets.top
  50.                 right  + parentInsets.left + childInsets.right
  51.                 bottom + parentInsets.top  + childInsets.bottom); 
  52.     } 
  53.     // 回到調用原處 
  54. /** 
  55.   * 分析3:onLayout() 
  56.   * 注:對于單一View的laytou過程 
  57.   *    1. 由于單一View是沒有子View的,故onLayout()是一個空實現 
  58.   *    2. 由于在layout()中已經對自身View進行了位置計算:setFrame() / setOpticalFrame() 
  59.   *    3. 所以單一View的layout過程在layout()后就已完成了 
  60.   */  
  61.  protected void onLayout(boolean changed, int leftint topint rightint bottom) { 
  62.    // 參數說明 
  63.    // changed 當前View的大小和位置改變了  
  64.    // left 左部位置 
  65.    // top 頂部位置 
  66.    // right 右部位置 
  67.    // bottom 底部位置 
  68. }  

3、setFrame

layout方法是用來確定自身位置的,其內部調用了setOpticalFrame、setFrame和onLayout方法,setOpticalFrame內部又會調用setFrame。所以我們先來看setFrame方法,如下

  1. protected boolean setFrame(int leftint topint rightint bottom) { 
  2.         boolean changed = false
  3.         if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) { 
  4.             //判斷View的位置是否發(fā)生改變 
  5.             changed = true
  6.             // Remember our drawn bit 
  7.             int drawn = mPrivateFlags & PFLAG_DRAWN; 
  8.             int oldWidth = mRight - mLeft;//獲取原來的寬度 
  9.             int oldHeight = mBottom - mTop;//獲取原來的高度 
  10.             int newWidth = right - left;//獲取新的寬度 
  11.             int newHeight = bottom - top;//獲取新的高度 
  12.             //判斷View的尺寸是否發(fā)生改變 
  13.             boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight); 
  14.             // Invalidate our old position 
  15.             invalidate(sizeChanged); 
  16.             //對mLeft、mTop、mRight 、mBottom初始化,View自身的位置也就確定了。 
  17.             mLeft = left
  18.             mTop = top
  19.             mRight = right
  20.             mBottom = bottom; 
  21.             mRenderNode.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom); 
  22.             mPrivateFlags |= PFLAG_HAS_BOUNDS; 
  23.             //如果View尺寸發(fā)生改變,將執(zhí)行View#sizeChange方法,在sizeChange方法內部會調用View#onSizeChanged方法。 
  24.             if (sizeChanged) { 
  25.                 sizeChange(newWidth, newHeight, oldWidth, oldHeight); 
  26.             } 
  27.             //省略... 
  28.         } 
  29.         return changed; 
  30.     } 

在setFrame方法中對mLeft、mTop、mRight 、mBottom進行初始化,mLeft、mTop分別對應View左上角的橫坐標和縱坐標,mRight 、mBottom分別對應了View右下角的橫坐標和縱坐標,View的四個頂點的坐標確定了,View自身的位置也就確定了;

4、FrameLayout#onLayout

再回到layout方法,在通過setFrame方法確定了自身位置后,接下來會調用onLayout方法,這個方法其實用來確定子View的位置的;

不過View和ViewGroup都沒有真正實現onLayout,因為onLayout和onMeasure類似,其過程都與具體的布局有關;

以FrameLayout為例來分析onLayout過程,FrameLayout#onLayout

  1. @Override 
  2.     protected void onLayout(boolean changed, int leftint topint rightint bottom) { 
  3.         layoutChildren(lefttopright, bottom, false /* no force left gravity */); 
  4.     } 
  5. 其內部調用了layoutChildren方法 
  6. void layoutChildren(int leftint topint rightint bottom, 
  7.                                   boolean forceLeftGravity) { 
  8.         final int count = getChildCount();//獲取子View的數量 
  9.         //parentLeft、parentTop分別代表子View所占區(qū)域左上角的橫坐標和縱坐標 
  10.         //parentRight、parentBottom分別代表子View所占區(qū)域右下角的橫坐標和縱坐標 
  11.         final int parentLeft = getPaddingLeftWithForeground(); 
  12.         final int parentRight = right - left - getPaddingRightWithForeground(); 
  13.         final int parentTop = getPaddingTopWithForeground(); 
  14.         final int parentBottom = bottom - top - getPaddingBottomWithForeground(); 
  15.         mForegroundBoundsChanged = true
  16.         //遍歷子View 
  17.         for (int i = 0; i < count; i++) { 
  18.             final View child = getChildAt(i); 
  19.             if (child.getVisibility() != GONE) { 
  20.                 final LayoutParams lp = (LayoutParams) child.getLayoutParams(); 
  21.                 //獲取子View的測量寬、高 
  22.                 final int width = child.getMeasuredWidth(); 
  23.                 final int height = child.getMeasuredHeight(); 
  24.                 int childLeft; 
  25.                 int childTop; 
  26.                 //獲取子View 設置的Gravity,如果子View沒有設置Gravity,則用默認的Gravity:DEFAULT_CHILD_GRAVITY。 
  27.                 int gravity = lp.gravity; 
  28.                 if (gravity == -1) { 
  29.                     gravity = DEFAULT_CHILD_GRAVITY; 
  30.                 } 
  31.                 final int layoutDirection = getLayoutDirection(); 
  32.                 final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection); 
  33.                 final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK; 
  34.                 //水平方向上,通過設置的Gravity,來確定childLeft,即每個子View左上角的橫坐標 
  35.                 switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) { 
  36.                     case Gravity.CENTER_HORIZONTAL: 
  37.                         childLeft = parentLeft + (parentRight - parentLeft - width) / 2 + 
  38.                         lp.leftMargin - lp.rightMargin; 
  39.                         break; 
  40.                     case Gravity.RIGHT
  41.                         if (!forceLeftGravity) { 
  42.                             childLeft = parentRight - width - lp.rightMargin; 
  43.                             break; 
  44.                         } 
  45.                     case Gravity.LEFT
  46.                     default
  47.                         childLeft = parentLeft + lp.leftMargin; 
  48.                 } 
  49.                 //豎直方向上,通過設置的Gravity,來確定childTop,即每個子View左上角的縱坐標 
  50.                 switch (verticalGravity) { 
  51.                     case Gravity.TOP
  52.                         childTop = parentTop + lp.topMargin; 
  53.                         break; 
  54.                     case Gravity.CENTER_VERTICAL: 
  55.                         childTop = parentTop + (parentBottom - parentTop - height) / 2 + 
  56.                         lp.topMargin - lp.bottomMargin; 
  57.                         break; 
  58.                     case Gravity.BOTTOM: 
  59.                         childTop = parentBottom - height - lp.bottomMargin; 
  60.                         break; 
  61.                     default
  62.                         childTop = parentTop + lp.topMargin; 
  63.                 } 
  64.                 //調用子View的layout 方法 
  65.                 child.layout(childLeft, childTop, childLeft + width, childTop + height); 
  66.             } 
  67.         } 
  68.     } 

在該方法內部遍歷所有子View過程中,通過子View設置的Gravity,獲去其childLeft、childTop即子View的左上角的橫坐標和縱坐標,最后執(zhí)行子View的layout方法,來確定子View的位置

5、LinearLayout#onLayout

LinearLayout復寫的onLayout()分析

  1. /** 
  2.   * 源碼分析:LinearLayout復寫的onLayout() 
  3.   * 注:復寫的邏輯 和 LinearLayout measure過程的 onMeasure()類似 
  4.   */  
  5.   @Override 
  6.   protected void onLayout(boolean changed, int l, int t, int r, int b) { 
  7.       // 根據自身方向屬性,而選擇不同的處理方式 
  8.       if (mOrientation == VERTICAL) { 
  9.           layoutVertical(l, t, r, b); 
  10.       } else { 
  11.           layoutHorizontal(l, t, r, b); 
  12.       } 
  13.   } 
  14.       // 由于垂直/水平方向類似,所以此處僅分析垂直方向(Vertical)的處理過程 ->分析1 
  15. /** 
  16.   * 分析1:layoutVertical(l, t, r, b) 
  17.   */ 
  18.   void layoutVertical(int leftint topint rightint bottom) { 
  19.       // 子View的數量 
  20.       final int count = getVirtualChildCount(); 
  21.       // 1. 遍歷子View 
  22.       for (int i = 0; i < count; i++) { 
  23.           final View child = getVirtualChildAt(i); 
  24.           if (child == null) { 
  25.               childTop += measureNullChild(i); 
  26.           } else if (child.getVisibility() != GONE) { 
  27.               // 2. 計算子View的測量寬 / 高值 
  28.               final int childWidth = child.getMeasuredWidth(); 
  29.               final int childHeight = child.getMeasuredHeight(); 
  30.               // 3. 確定自身子View的位置 
  31.               // 即:遞歸調用子View的setChildFrame(),實際上是調用了子View的layout() ->分析2 
  32.               setChildFrame(child, childLeft, childTop + getLocationOffset(child), 
  33.                       childWidth, childHeight); 
  34.               // childTop逐漸增大,即后面的子元素會被放置在靠下的位置 
  35.               // 這符合垂直方向的LinearLayout的特性 
  36.               childTop += childHeight + lp.bottomMargin + getNextLocationOffset(child); 
  37.               i += getChildrenSkipCount(child, i); 
  38.           } 
  39.        } 
  40.     } 
  41. /** 
  42.   * 分析2:setChildFrame() 
  43.   */ 
  44.   private void setChildFrame( View child, int leftint topint width, int height){ 
  45.     child.layout(lefttopleft ++ width, top + height); 
  46.     // setChildFrame()僅僅只是調用了子View的layout()而已 
  47.     // 在子View的layout()又通過調用setFrame()確定View的四個頂點 
  48.     // 即確定了子View的位置 
  49.     // 如此不斷循環(huán)確定所有子View的位置,最終確定ViewGroup的位置 
  50.   } 

總結 

View的layout流程核心在于覆寫ViewGroup的onLayout方法,它的流程是拿到子View的寬高,然后實現自己的布局子View的邏輯,它一般結合onMeasure方法使用。

 

責任編輯:武曉燕 來源: Android開發(fā)編程
相關推薦

2021-09-16 06:44:04

Android進階流程

2021-09-30 07:36:51

AndroidViewDraw

2021-09-10 07:31:54

AndroidAppStartup原理

2021-09-08 06:51:52

AndroidRetrofit原理

2021-10-15 09:19:17

AndroidSharedPrefe分析源碼

2022-09-05 22:22:00

Stream操作對象

2022-10-11 07:43:34

AndroidSyncGradle 構建

2021-08-24 07:53:28

AndroidActivity生命周期

2021-10-21 10:02:37

Java開發(fā)代碼

2021-10-10 13:31:14

Java負載均衡算法

2024-05-23 08:02:23

2021-09-15 07:31:33

Android窗口管理

2014-07-15 17:17:31

AdapterAndroid

2021-09-24 08:10:40

Java 語言 Java 基礎

2017-08-08 09:15:41

前端JavaScript頁面渲染

2021-10-26 17:52:52

Android插件化技術

2021-02-17 11:25:33

前端JavaScriptthis

2022-01-14 12:28:18

架構OpenFeign遠程

2022-07-06 08:05:52

Java對象JVM

2022-11-04 09:43:05

Java線程
點贊
收藏

51CTO技術棧公眾號