Android Measure流程(二)

101 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第2天,点击查看活动详情

导航:Android Measure流程(一)juejin.cn/post/716850…

Measure中的Spec参数从何而来

当然是debug了

直接de

微信截图_20221123214242.png

从上图的调用栈可以看出由performMeasure调用的DecorView的measure->onMeasure DercorView的onMeasure主要是计算measureSpec,由于DercorView继承了FrameLayout,计算好measureSpec后调用了super.onMeasure,也就是FrameLayout的onMeasure。

我们看一下FrameLayout的onMeasure

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int count = getChildCount();

    final boolean measureMatchParentChildren =
            MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||
            MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
    //清除mMatchParentChildren数组
    mMatchParentChildren.clear();

    int maxHeight = 0;
    int maxWidth = 0;
    int childState = 0;

    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        //mMeasureAllChildren意思是是否考虑全部子节点默认为false即只考虑可见子节点
        if (mMeasureAllChildren || child.getVisibility() != GONE) {
        //这里是关键,也正是这里调用到了LinearLayout.measure(由于没有重写measure所以我们看LinearLayout.onMeasure)
        //measureChildWithMargins中的参数:heigth/wiethUsed意思是这一部分被被当前layout占用,不允许childView占用,可以看作padding
            measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            maxWidth = Math.max(maxWidth,
                    child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
            maxHeight = Math.max(maxHeight,
                    child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
            childState = combineMeasuredStates(childState, child.getMeasuredState());
            if (measureMatchParentChildren) {
                if (lp.width == LayoutParams.MATCH_PARENT ||
                        lp.height == LayoutParams.MATCH_PARENT) {
                    mMatchParentChildren.add(child);
                }
            }
        }
    }

    // Account for padding too
    maxWidth += getPaddingLeftWithForeground() + getPaddingRightWithForeground();
    maxHeight += getPaddingTopWithForeground() + getPaddingBottomWithForeground();

    // Check against our minimum height and width
    maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
    maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());

    // Check against our foreground's minimum height and width
    final Drawable drawable = getForeground();
    if (drawable != null) {
        maxHeight = Math.max(maxHeight, drawable.getMinimumHeight());
        maxWidth = Math.max(maxWidth, drawable.getMinimumWidth());
    }
//设置自己的测量结果
    setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
            resolveSizeAndState(maxHeight, heightMeasureSpec,
                    childState << MEASURED_HEIGHT_STATE_SHIFT));

//如果parent的长度和或宽度都不是确定的,同时如果child存在MATCH_PARENT,则会重新测量。因为当前layout的长度或者宽度可能用传进来的measureSpec确定了,比如上面的child都指定了自己的大小,那么当前layout的宽度和高度就确定了。
    count = mMatchParentChildren.size();
    if (count > 1) {
        for (int i = 0; i < count; i++) {
            final View child = mMatchParentChildren.get(i);
            final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

            final int childWidthMeasureSpec;
            if (lp.width == LayoutParams.MATCH_PARENT) {
                final int width = Math.max(0, getMeasuredWidth()
                        - getPaddingLeftWithForeground() - getPaddingRightWithForeground()
                        - lp.leftMargin - lp.rightMargin);
                childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
                        width, MeasureSpec.EXACTLY);
            } else {
                childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
                        getPaddingLeftWithForeground() + getPaddingRightWithForeground() +
                        lp.leftMargin + lp.rightMargin,
                        lp.width);
            }

            final int childHeightMeasureSpec;
            if (lp.height == LayoutParams.MATCH_PARENT) {
                final int height = Math.max(0, getMeasuredHeight()
                        - getPaddingTopWithForeground() - getPaddingBottomWithForeground()
                        - lp.topMargin - lp.bottomMargin);
                childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
                        height, MeasureSpec.EXACTLY);
            } else {
                childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
                        getPaddingTopWithForeground() + getPaddingBottomWithForeground() +
                        lp.topMargin + lp.bottomMargin,
                        lp.height);
            }

            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        }
    }
}

也就是说如果framelayout的直接子view不对自己warp_parent作限制的话,而且framlayout也是warp_parent,那么该view会填充整个framlayout,那么framlayout的测量结果也会和parent一样。

几个很重要的measure函数

  • viewGroup中的measureChildWithMargins (View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed)

    具体实现:

final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
        mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
                + widthUsed, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
        mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
                + heightUsed, lp.height);

child.measure(childWidthMeasureSpec, childHeightMeasureSpec);

作用:使参数中的child测量自己,如同函数名称一样,同时需要考虑到parent的填充和child的边距还有width/heightUsed(parent需要额外占据的大小)。

但是关于这些繁重的操作其实是内部调用了下面这个函数,由它来完成。

  • viewGroup中的getChildMeasureSpec(int spec, int padding, int childDimension)

    具体实现:

public static int getChildMeasureSpec(int spec, int padding, int childDimension) {

    //解析parent的spec
    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的大小已经指定
    case MeasureSpec.EXACTLY:
        if (childDimension >= 0) {//如果child也已经指定大小(因为warp(match)_parent是负数)
            resultSize = childDimension;//那么就直接设置为自己的大小
            resultMode = MeasureSpec.EXACTLY;
        } else if (childDimension == LayoutParams.MATCH_PARENT) {
            
            resultSize = size;//和parent的可用宽度一样大
            resultMode = MeasureSpec.EXACTLY;
        } else if (childDimension == LayoutParams.WRAP_CONTENT) {
            
            resultSize = size;//child不确定自己的大小,由于不会超过parent的可用大小,则先指定为size
            resultMode = MeasureSpec.AT_MOST;
        }
        break;

    // parent未指定大小
    case MeasureSpec.AT_MOST:
        if (childDimension >= 0) {//child大小已经确定
            
            resultSize = childDimension;
            resultMode = MeasureSpec.EXACTLY;
        } else if (childDimension == LayoutParams.MATCH_PARENT) {
            .
            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;
        }
        break;

    // parent未指定大小
    case MeasureSpec.UNSPECIFIED:
        if (childDimension >= 0) {
            // Child wants a specific size... let them 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;
    }
    //返回测量后的Spec,这个函数的作用就是把seize和mode转换成spec
    return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
}

从调用栈可知,measure是从viewroootimpl开始的, 下文我们再关注一下viewroootimpl