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

從自定義ViewGroup看Layout作用

開發(fā) 前端
關(guān)于layout,很多朋友知道它是負責布局的,那么具體是怎么布局的?viewGroup和view的layout方法又有什么不同?一起來看看吧。

[[400334]]

我回來了

這次感冒可耽誤我太多時間了,中間斷斷續(xù)續(xù)去了幾趟醫(yī)院和診所,終于差不多好了,于是心里又暗暗下定決定,一定要好好養(yǎng)身體(可能過兩天又忘了??)

總之大家也都多注意身體吧,身體垮了啥也干不了。

廢話不多說,開始今天的Android之旅~

前言

上次我們說到View的Mearsure流程,今天接著說說layout。

關(guān)于layout,很多朋友知道它是負責布局的,那么具體是怎么布局的?viewGroup和view的layout方法又有什么不同?一起來看看吧。

View layout方法

首先,還是從ViewRootImpl說起,界面的繪制會觸發(fā)performMeasure、performLayout方法,而在performLayout方法中就會調(diào)用mView的layout方法開始一層層View的布局工作。

  1. private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth, 
  2.            int desiredWindowHeight) { 
  3.         
  4.        final View host = mView; 
  5.        host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight()); 
  6.    } 

mView我們都知道了,就是頂層View——DecorView,那么就進去看看DecorView的layout方法:

不好意思,DecorView中并沒有l(wèi)ayout方法...

所以,我們直接看看View的layout方法:

  1. public void layout(int l, int t, int r, int b) { 
  2.  
  3.        boolean changed = isLayoutModeOptical(mParent) ? 
  4.                setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b); 
  5.  
  6.        if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) { 
  7.            onLayout(changed, l, t, r, b); 
  8.        } 
  9.    } 
  10.  
  11.    protected void onLayout(boolean changed, int leftint topint rightint bottom) { 
  12.    } 
  • 首先,方法傳入了四個參數(shù),分別代表view的左、上、下、右四個值。
  • 然后通過setOpticalFrame方法或者setFrame方法判斷布局參數(shù)是否改變。

具體判斷過程就是通過老的上下左右值和新的上下左右值進行比較,邏輯就在setFrame方法中:

  1. protected boolean setFrame(int leftint topint rightint bottom) { 
  2.         boolean changed = false
  3.  
  4.         if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) { 
  5.             changed = true
  6.  
  7.             // Remember our drawn bit 
  8.             int drawn = mPrivateFlags & PFLAG_DRAWN; 
  9.  
  10.             int oldWidth = mRight - mLeft; 
  11.             int oldHeight = mBottom - mTop; 
  12.             int newWidth = right - left
  13.             int newHeight = bottom - top
  14.             boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight); 
  15.  
  16.             // Invalidate our old position 
  17.             invalidate(sizeChanged); 
  18.  
  19.             mLeft = left
  20.             mTop = top
  21.             mRight = right
  22.             mBottom = bottom; 
  23.             mRenderNode.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom); 
  24.         } 
  25.         return changed; 
  26.     } 

如果上下左右有一個參數(shù)值發(fā)生了改變,就說明這個View的布局發(fā)生了改變,然后重新計算View的寬度高度(newWidth、newHeight),并賦值了View新的上下左右參數(shù)值。

在這個layout方法中主要涉及到了四個參數(shù):mLeft、mTop、mBottom、mRight,分別代表了View的左坐標、上坐標、下坐標和右坐標,你可以把View理解為一個矩形,確定了這四個值,就能確定View矩形的四個頂點值,也就能確定View在畫布中的具體位置。

所以,layout方法到底干了啥?

就是傳入上下左右值、然后賦值上下左右值、完畢。

然后我們就可以根據(jù)這些值獲取View的一系列參數(shù),比如View寬度:

  1. public final int getWidth() { 
  2.       return mRight - mLeft; 
  3.   } 

至此,View的layout方法就結(jié)束了,主要就是通過對上下左右參數(shù)的賦值完成對View的布局,非常簡單。

下面看看ViewGroup。

ViewGroup layout方法

  1. @Override 
  2.    public final void layout(int l, int t, int r, int b) { 
  3.        if (!mSuppressLayout && (mTransition == null || !mTransition.isChangingLayout())) { 
  4.            if (mTransition != null) { 
  5.                mTransition.layoutChange(this); 
  6.            } 
  7.            super.layout(l, t, r, b); 
  8.        } else { 
  9.            mLayoutCalledWhileSuppressed = true
  10.        } 
  11.    } 

額,還是調(diào)用到View的layout方法,難道說ViewGroup和View的布局過程是一樣的,就是確定了本身的位置?

那ViewGroup的子View怎么辦呢?不急,我們剛才說layout方法的時候還漏了一個onLayout方法,只不過這個方法在View里面是空實現(xiàn),而到了ViewGroup中變成了一個抽象方法:

  1. @Override 
  2.     protected abstract void onLayout(boolean changed, 
  3.             int l, int t, int r, int b); 

也就是任何ViewGroup都必須實現(xiàn)這個方法,來完成對子View的布局擺放。

具體的布局擺放邏輯就是在onLayout方法中一個個調(diào)用子View的layout方法,然后完成每個子View的布局,最終完成繪制工作。

接下來我們就來自己實現(xiàn)一個垂直線性布局(類似LinearLayout),正好復習下上一節(jié)的onMearsure和這一節(jié)的onLayout。

自定義垂直布局VerticalLayout

首先,我們要確定我們這個自定義ViewGroup的作用,是類似垂直方向的LinearLayout功能,在該ViewGroup下的子View可以按垂直線性順序依次往下排放。我們給它起個名字叫VerticalLayout~

繼承ViewGroup

首先,我們這個布局肯定要繼承自ViewGroup,并且實現(xiàn)相應的構(gòu)造方法:

  1. public class VerticalLayout : ViewGroup { 
  2.  
  3.     constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int = 0) : super( 
  4.         context, 
  5.         attrs, 
  6.         defStyleAttr 
  7.     ) 
  8.  
  9.     constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { 
  10.     } 

重寫generateLayoutParams方法

自定義ViewGroup還需要重寫的一個方法是generateLayoutParams,這一步是為了讓我們的ViewGroup支持Margin,后續(xù)我們就可以通過MarginLayoutParams來獲取子View的Margin值。

  1. override fun generateLayoutParams(attrs: AttributeSet?): LayoutParams? { 
  2.       return MarginLayoutParams(context, attrs) 
  3.   } 

重寫測量方法onMeasure

然后,我們需要對我們的布局進行測量,也就是重寫onMeasure方法。

在該方法中,我們需要對我們的布局進行測量,并且將測量好的寬高傳入setMeasuredDimension方法,完成測量。

  1. protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) 

之前我們說過,onMeasure方法會傳進來兩個參數(shù),widthMeasureSpec和heightMeasureSpec。

里面包含了父View根據(jù)當前View的LayoutParams和父View的測量規(guī)格進行計算,得出的對當前View期望的測量模式和測量大?。?/p>

  • 當測量模式為MeasureSpec.EXACTLY

也就是當寬或者高為確定值時,那么當前布局View的寬高也就是設定為父View給我們設置好的測量大小即可。比如寬為400dp,那么我們無需重新測量直接調(diào)用setMeasuredDimension傳入這個固定值即可。

  • 當測量模式為MeasureSpec.AT_MOST 或者 UNSPECIFIED:

這時候,說明父View對當前View的要求不固定,是可以為任意大小或者不超過最大值的情況,比如設置這個VerticalLayout的高度為wrap_content。那么我們就必須重新進行高度測量了,因為只有我們設計者知道這個自適應高度需要怎么計算。具體就是VerticalLayout是一個垂直線性布局,所以高度很自然就是所有子View的高度之和。

至此,onMeasure方法的邏輯也基本摸清了:

  1. override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { 
  2.         super.onMeasure(widthMeasureSpec, heightMeasureSpec) 
  3.         //獲取寬高的測量模式和測量大小 
  4.         val widthMode = MeasureSpec.getMode(widthMeasureSpec) 
  5.         val heightMode = MeasureSpec.getMode(heightMeasureSpec) 
  6.         val sizeWidth = MeasureSpec.getSize(widthMeasureSpec) 
  7.         val sizeHeight = MeasureSpec.getSize(heightMeasureSpec) 
  8.  
  9.         var mHeight = 0 
  10.         var mWidth = 0 
  11.  
  12.         //遍歷子View,獲取總高度 
  13.         for (i in 0 until childCount) { 
  14.             val childView = getChildAt(i) 
  15.             //測量子View的寬和高 
  16.             measureChild(childView, widthMeasureSpec, heightMeasureSpec) 
  17.             val lp = childView.layoutParams as MarginLayoutParams 
  18.             val childWidth = childView.measuredWidth + lp.leftMargin + lp.rightMargin 
  19.             val childHeight = childView.measuredHeight + lp.topMargin + lp.bottomMargin 
  20.  
  21.             //計算得出最大寬度 
  22.             mWidth = Math.max(mWidth, childWidth) 
  23.             //累計計算高度 
  24.             mHeight += childHeight 
  25.         } 
  26.  
  27.         //設置寬高 
  28.         setMeasuredDimension( 
  29.             if (widthMode == MeasureSpec.EXACTLY) sizeWidth else mWidth, 
  30.             if (heightMode == MeasureSpec.EXACTLY) sizeHeight else mHeight 
  31.         ) 
  32.     } 

主要的邏輯就是遍歷子View,得出VerticalLayout的實際寬高:

最終ViewGroup的高 = 所有子View的 (高 + margin值)

最終ViewGroup的寬 = 最大子View的 (寬 + margin值)

最后調(diào)用setMeasuredDimension 根據(jù)測量模式 傳入寬高。

重寫布局方法onLayout

上文說過,作為一個ViewGroup,必須重寫onLayout方法,來保證子View的正常布局擺放。

垂直線性布局VerticalLayout亦是如此,那么在這個布局中onLayout方法的關(guān)鍵邏輯又是什么呢?

還是那句話,確定位置,也就是確定左、上、右、下四個參數(shù)值,而在VerticalLayout中,最關(guān)鍵的參數(shù)就是這個上,也就是top值。

每個View的top值必須是上一個View的bottom值,也就是接著上一個View進行擺放,這樣才會是垂直線性的效果,所以我們需要做的就是動態(tài)計算每個View的top值,其實也就是不斷累加View的高度,作為下一個View的top值。

  1. override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) { 
  2.         var childWidth = 0 
  3.         var childHeight = 0 
  4.         var childTop = 0 
  5.         var lp: MarginLayoutParams 
  6.  
  7.         //遍歷子View,布局每個子View 
  8.         for (i in 0 until childCount) { 
  9.             val childView = getChildAt(i) 
  10.             childHeight = childView.measuredHeight 
  11.             childWidth = childView.measuredWidth 
  12.             lp = childView.layoutParams as MarginLayoutParams 
  13.  
  14.             //累計計算top值 
  15.             childTop += lp.topMargin 
  16.  
  17.             //布局子View 
  18.             childView.layout( 
  19.                 lp.leftMargin, 
  20.                 childTop, 
  21.                 lp.leftMargin + childWidth, 
  22.                 childTop + childHeight 
  23.             ); 
  24.  
  25.             childTop += childHeight + lp.bottomMargin 
  26.         } 
  27.     } 

邏輯還是挺簡單的,

left是固定的子View的leftMargin。

top是累加計算的子View的高度 + Margin值。

right是left + 子View的寬度。

bottom是top + 子View的高度。

最后調(diào)用子View的layout方法,對每個子View進行布局。

大功告成,最后看看我們這個自定義垂直線性布局的效果吧~

效果展示

  1. <com.panda.studynote3.VerticalLayout 
  2.         android:layout_width="wrap_content" 
  3.         android:layout_height="wrap_content"
  4.  
  5.         <TextView 
  6.             android:layout_width="100dp" 
  7.             android:layout_height="100dp" 
  8.             android:text="啦啦啦" 
  9.             android:textSize="20sp" 
  10.             android:textColor="@color/white" 
  11.             android:background="@color/design_default_color_primary" 
  12.             /> 
  13.  
  14.         <TextView 
  15.             android:layout_width="300dp" 
  16.             android:layout_height="200dp" 
  17.             android:layout_marginTop="20dp" 
  18.             android:background="@color/cardview_dark_background" 
  19.             android:textSize="20sp" 
  20.             android:textColor="@color/white" 
  21.             android:text="你好啊" 
  22.             /> 
  23.  
  24.         <TextView 
  25.             android:layout_width="140dp" 
  26.             android:layout_height="100dp" 
  27.             android:text="嘻嘻" 
  28.             android:layout_marginLeft="10dp" 
  29.             android:layout_marginTop="10dp" 
  30.             android:textSize="20sp" 
  31.             android:gravity="center" 
  32.             android:textColor="@color/black" 
  33.             android:background="@color/teal_200" 
  34.             /> 
  35.  
  36.     </com.panda.studynote3.VerticalLayout> 

本文轉(zhuǎn)載自微信公眾號「碼上積木」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系碼上積木公眾號。

 

責任編輯:武曉燕 來源: 碼上積木
相關(guān)推薦

2015-02-12 15:33:43

微信SDK

2020-11-25 11:20:44

Spring注解Java

2015-02-12 15:38:26

微信SDK

2021-07-05 08:43:46

Spring Beanscope作用域

2016-12-26 15:25:59

Android自定義View

2016-11-16 21:55:55

源碼分析自定義view androi

2011-06-23 10:49:13

Qt 自定義信號

2017-02-28 10:05:56

Chrome源碼

2023-09-05 08:23:56

SpringScope方法

2021-12-07 18:23:50

自定義進度條分段式

2025-03-03 00:00:00

Chrome工具前端

2009-07-06 16:59:26

JSP自定義標簽

2011-12-16 14:23:51

Java

2015-01-14 15:06:48

定義相機

2009-06-08 20:13:36

Eclipse自定義控

2022-04-24 15:17:56

鴻蒙操作系統(tǒng)

2021-11-23 15:06:42

Kubernetes 運維開源

2013-04-01 14:35:10

Android開發(fā)Android自定義x

2015-07-22 10:57:36

watchOS圖表自定義

2010-09-14 16:47:23

SQL自定義函數(shù)
點贊
收藏

51CTO技術(shù)棧公眾號