view的measure流程

本文详细解析了Android中View和ViewGroup的测量流程,包括onMeasure方法、setMeasuredDimension方法、getDefaultSize方法的工作原理,以及ViewGroup如何通过measureChildren和measureChild方法测量其子View。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

view的measure流程

1.View的measure流程

  • view的measure流程实在onMeasure()方法完成的,源码如下:
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
    }

  • 接下来看下setMeasuredDimension()方法
   protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
        boolean optical = isLayoutModeOptical(this);
        if (optical != isLayoutModeOptical(mParent)) {
            Insets insets = getOpticalInsets();
            int opticalWidth  = insets.left + insets.right;
            int opticalHeight = insets.top  + insets.bottom;

            measuredWidth  += optical ? opticalWidth  : -opticalWidth;
            measuredHeight += optical ? opticalHeight : -opticalHeight;
        }
        setMeasuredDimensionRaw(measuredWidth, measuredHeight);
    }
  • 然后是setMeasuredDimensionRaw()方法
    private void setMeasuredDimensionRaw(int measuredWidth, int measuredHeight) {
        mMeasuredWidth = measuredWidth;
        mMeasuredHeight = measuredHeight;

        mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
    }
  • 其实它就是为了设置宽和高,然后我们回到onMeasure()方法中,再看下setMeasuredDimension()中获取传入参数的方法getDefaultSize()
  public static int getDefaultSize(int size, int measureSpec) {
        int result = size;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        switch (specMode) {
        case MeasureSpec.UNSPECIFIED:
            result = size;
            break;
        case MeasureSpec.AT_MOST:
        case MeasureSpec.EXACTLY:
            result = specSize;
            break;
        }
        return result;
    }
  • getDefaultSize()就是根据不同的SpecMode返回不同的SpecSize.
  • 同时,在源码中我们可以看到当模式是AT_MOST和EXACTLY时,它的返回值是一样的,View在这两种模式下的测量宽和高直接取决于SpecSize.也就是说, 对于一个直接继承自View的自定义View来说,它的wrap_content和 match_parent 属性的效果是一样的.因此如果要实现自定义 View的wrap_content,则要重写onMeasure方法,并对自定义View的wrap_content属性进行处理
  • 当模式是UNSPECIFIED的时候,返回的是size,我们看下size的值是如何获取的即getSuggestedMinimumWidth()或者getSuggestedMinimumHeight()方法的返回值,这两个方法原理一样,我们看getSuggestedMinimumWidth()这个方法
 protected int getSuggestedMinimumWidth() {
        return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
    }
  • 这段代码比较好理解,如果view没有设置背景,那么就会返回mMinWidth.mMinWidth的值可以设置,它对应的是Android:mMinWidth这个属性,也可以在代码中使用setMinimumWidth()设置
  • 如果有背景,那就返回mMinWidth和mBackground.getMinimumWidth()之间的最大值
  • 下面来看下mBackground的getMinimumWidth()方法
  public int getMinimumWidth() {
        final int intrinsicWidth = getIntrinsicWidth();
        return intrinsicWidth > 0 ? intrinsicWidth : 0;
    }
  • 这里mBackground是Drawable类型的,intrinsicWidth得到的是这个Drawable的固有宽度,如果固有宽度大
    于0则返回固有宽度,否则返回0
  • 小结:getSuggestedMinimumWidth方法就是:如果View没有设置背景,则返回mMinWidth;如果设置了背景,就返回mMinWidth和Drawable的最小宽度之间的最大值

2.ViewGroup的measure流程

  • ViewGroup中没有onMeasure方法,但是它有measureChildren()方法
   protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
        final int size = mChildrenCount;
        final View[] children = mChildren;
        for (int i = 0; i < size; ++i) {
            final View child = children[i];
            if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
                measureChild(child, widthMeasureSpec, heightMeasureSpec);
            }
        }
    }
  • 从源码看出,就是遍历子view然后调用measureChild()方法
    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);
    }
  • 这里面是获取子view的LayoutParams参数和子view的MeasureSpec,然后调用子view的measure()方法进行测量,先看下getChildMeasureSpec()方法
  public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
        int specMode = MeasureSpec.getMode(spec);
        int specSize = MeasureSpec.getSize(spec);

        int size = Math.max(0, specSize - padding);

        int resultSize = 0;
        int resultMode = 0;

        switch (specMode) {
        // Parent has imposed an exact size on us
        case MeasureSpec.EXACTLY:
            if (childDimension >= 0) {
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size. So be it.
                resultSize = size;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size. It can't be
                // bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            }
            break;

        // Parent has imposed a maximum size on us
        case MeasureSpec.AT_MOST:
            if (childDimension >= 0) {
                // Child wants a specific size... so be it
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size, but our size is not fixed.
                // Constrain child to not be bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size. It can't be
                // bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST; // 1
            }
            break;

        // Parent asked to see how big we want to be
        case MeasureSpec.UNSPECIFIED:
            if (childDimension >= 0) {
                // Child wants a specific size... let him have it
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size... find out how big it should
                // be
                resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
                resultMode = MeasureSpec.UNSPECIFIED;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size.... find out how
                // big it should be
                resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
                resultMode = MeasureSpec.UNSPECIFIED;
            }
            break;
        }
        //noinspection ResourceType
        return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
    }
  • 根据源码,可以很明显的看出getChildMeasureSpec()方法返回的是根据传进来的父view的MeasureSpec和子view的LayoutParams得出的子view的MeasureSpec值
  • 需要注意的一点是,根据注释1处的代码,如果父view的模式是AT_MOST且子view的LayoutParams是wrap_content,得出的MeasureSpec和子view的LayoutParams是match_parent得出的MeasureSpec值是一样的,因此,为了解决这个问题,需要在LayoutParams属性为WRAP_CONTENT时指定一下默认的宽和高
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值