onMeasure小结

Measure

假设我们自定义一个 ViewGroup 和一个 View,View放在ViewGroup里。
现在将Measure分成两个部分,ViewGroup部分和 View部分,因为View的Measure受ViewGroup影响,所以这里先从ViewGroup开始分析

ViewGroup

**重写onMeasure的作用:计算子控件的尺寸和模式,以及设置自己的宽和高:
**

既然要获得子控件的尺寸,就要先测量子控件,调用 viewGroup 的 measureChild(child, widthMeasureSpec, heightMeasureSpec) 方法

**measureChild(child, widthMeasureSpec, heightMeasureSpec); **
作用: 测量子控件,如果不调用该方法,父容器将不测量子控件
参数:
child:要测量的子控件
widthMeasureSpec:当前父容器的widthMeasureSpec
heightMeasureSpec:当前父容器的heightMeasureSpec

源码:
ViewGroup 的 measureChild(View child, int parentWidthMeasureSpec , int parentHeightMeasureSpec) 方法

    protected void measureChild(View child, int parentWidthMeasureSpec,
            int parentHeightMeasureSpec) {
        final LayoutParams lp = child.getLayoutParams();

        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,mPaddingLeft + mPaddingRight, lp.width);
        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,mPaddingTop + mPaddingBottom, lp.height);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }

1.得到子 View 的 MeasureSpec
2.调用子 View 的 measure(int widthMeasureSpec, int heightMeasureSpec) 方法,并传入前面的到的 MeasureSpec

View 的 measure(int widthMeasureSpec, int heightMeasureSpec) 方法

public final void measure(int widthMeasureSpec, int heightMeasureSpec) {

   boolean optical = isLayoutModeOptical(this);

   if (optical != isLayoutModeOptical(mParent)) {

   Insets insets = getOpticalInsets();

   int oWidth = insets.left + insets.right;
   int oHeight = insets.top + insets.bottom;

   widthMeasureSpec = MeasureSpec.adjust(widthMeasureSpec, optical ? -oWidth : oWidth);
   heightMeasureSpec = MeasureSpec.adjust(heightMeasureSpec, optical ? -oHeight : oHeight);

  }

    ...

    if (cacheIndex < 0 || sIgnoreMeasureCache) {

        onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    ...

 }

1.调用子 View 的 onMeasure 方法,并将 MeasureSpec 传给子 View

子控件 onMeasure 方法中的 MeasureSpec 就是这样来的

View

从上面的分析中得知 子控件的 MeasureSpec 的创建会受到父容器的 MeasureSec子控件自身的 LayoutParams 的影响。(父类影响子控件 MeasureSpec,子控件不会影响父类)

这时候通过 MeasureSpec 获得 SpecMode。
SpecMode三种模式:
UNSPECIFIED:父容器没有给子控件任何限制,子View可以设置为任意大小。
EXACTLY:父容器已经计算出子控件的具体尺寸(子控件设置了 match_parent 或 具体dp),这时候子控件的大小就等于 SpecSize 的值了
AT_MOST:子控件没有设置具体大小(设置 wrap_content),所以父容器建议最大 SpecSize 给当前控件,如果当前空间不设置大小(调用 setMeasuredDimension(int width , int height))方法,当前控件大小就是父容器建议最大 SpecSize

既然这个时候父容器已经计算好子控件的尺寸,还有必要重写 onMeasure 吗?
建议看看这篇文章Android 自定义View,就会有自己的理解了

小结

对于父容器(ViewGroup)而言,往往会重载onMeasure函数负责其children的measure工作,重载时不要忘记调用setMeasuredDimension来设置自身的mMeasuredWidth和mMeasuredHeight。

对于子控件(View)而言,通过调用系统默认的onMeasure,即可完成View的测量,当然你也可以重载onMeasure,并调用setMeasuredDimension来设置任意大小的布局。

你可能感兴趣的:(onMeasure小结)