View的工作原理-Measure

简介

自定义View必须要知道View的工作原理,我们都知道View的工作流程是measure->layout->draw,今天我们就逐个分析一下三个步骤。
首先要知道ViewRoot和Window和DecorView三者的关系,ViewRoot对应的是ViewRootImpl类,它是连接WindoManage和DecorView的纽带,View的整个工作流程都是在ViewRootImpl的performTraversals中完成的。
今天我们一起看看Measure的工作流程。

理解MeasureSpec

在View的Measure过程MeasureSpec是一个很重要的内容,它描述了要测量的尺寸和模式。View在Measure的过程是根据自身的LayoutParams和父容器的MeasureSpec转换成对应自身的MeasureSpec,然后再根据这个MeasureSpec测量出View的宽和高。
我们先看一下MeasureSpec的定义

public static class MeasureSpec {
        private static final int MODE_SHIFT = 30;
        private static final int MODE_MASK  = 0x3 << MODE_SHIFT;

        /** @hide */
        @IntDef({UNSPECIFIED, EXACTLY, AT_MOST})
        @Retention(RetentionPolicy.SOURCE)
        public @interface MeasureSpecMode {}

        /**
         * Measure specification mode: The parent has not imposed any constraint
         * on the child. It can be whatever size it wants.
         */
        public static final int UNSPECIFIED = 0 << MODE_SHIFT;

        /**
         * Measure specification mode: The parent has determined an exact size
         * for the child. The child is going to be given those bounds regardless
         * of how big it wants to be.
         */
        public static final int EXACTLY     = 1 << MODE_SHIFT;

        /**
         * Measure specification mode: The child can be as large as it wants up
         * to the specified size.
         */
        public static final int AT_MOST     = 2 << MODE_SHIFT;

        public static int makeMeasureSpec( int size, @MeasureSpecMode int mode) {
            if (sUseBrokenMakeMeasureSpec) {
                return size + mode;
            } else {
                return (size & ~MODE_MASK) | (mode & MODE_MASK);
            }
        }
        
        @UnsupportedAppUsage
        public static int makeSafeMeasureSpec(int size, int mode) {
            if (sUseZeroUnspecifiedMeasureSpec && mode == UNSPECIFIED) {
                return 0;
            }
            return makeMeasureSpec(size, mode);
        }
        
        @MeasureSpecMode
        public static int getMode(int measureSpec) {
            //noinspection ResourceType
            return (measureSpec & MODE_MASK);
        }
       
        public static int getSize(int measureSpec) {
            return (measureSpec & ~MODE_MASK);
        }

    }

MeasureSpec 代表一个32位的int值,高2位代表SpecMode,低30位代表SpecSize。
通过静态方法makeMeasureSpec将SpecMode和SpecSize打包成一个int值,避免过多的对象内存分配,另外还提供了解包方法。
SpecMode有三种模式:
**UNSPECIFIED **
父容器对view没有任何约束,view想要多大就多大,这种模式一般系统使用或者是scrollView使用。
EXACTLY
父容器检测出View需要的精确大小,View的大小是SpecSize指定的值。它对应于LayoutParams中的match_parent和具体的数值。
AT_MOST
父容器指定一个最大值SpecSize,View的大小不能超过该值,它对应的是LayoutParams的wrap_content

MeasureSpec在DecorView和View以及ViewGroup中的表现是不一样的,我们分别分析一下三者的区别。

DecorView的MeasureSpec

DecorView的MeasureSpec是由Window的尺寸和自身的LayoutParams共同决定的。在ViewRootImpl的measureHierarchy中展示了MeasureSpec的创建过程

  childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
  childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
  performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);

desiredWindowWidth和desiredWindowHeight表示屏幕的尺寸,接着看一下getRootMeasureSpec

  private static int getRootMeasureSpec(int windowSize, int rootDimension) {
        int measureSpec;
        switch (rootDimension) {

        case ViewGroup.LayoutParams.MATCH_PARENT:
            // Window can't resize. Force root view to be windowSize.
            measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
            break;
        case ViewGroup.LayoutParams.WRAP_CONTENT:
            // Window can resize. Set max size for root view.
            measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
            break;
        default:
            // Window wants to be an exact size. Force root view to be that size.
            measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
            break;
        }
        return measureSpec;
    }

到此可以明确的看出DecorView的MeasureSpec产生遵守如下规则:

  • MATCH_PARENT:精确模式,大小是窗口大小
  • WRAP_CONTENT: 最大模式,最大大小是窗口大小
  • 其他:精确模式,大小是LayoutParams指定的具体值。

View的MeasureSpec

先看一下ViewGroup的measureChildWithMargins

    protected void 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);
    }

上述方法对子元素进行measure,measure前的childXXXMeasureSpec是通过父容器的MeasureSpec,自身(ViewGroup)的被占用的空间(padding和margin),子View的LayoutParams综合测量计算出来的。我们看一下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) {
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            }
            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) {
                resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
                resultMode = MeasureSpec.UNSPECIFIED;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
                resultMode = MeasureSpec.UNSPECIFIED;
            }
            break;
        }
        //noinspection ResourceType
        return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
    }

方法内部主要是根据父容器的MeasureSpec和自身的LayoutParams来确定子元素的MeasureSpec,其中size是根据父容器的尺寸减去已经被占用的空间尺寸,及可用的尺寸。为了更清楚的展示getChildMeasureSpec的流程我们绘制一个表格:


View的工作原理-Measure_第1张图片
MeasureSpec.png

如果只是一个View的measure会调用onMeasure方法完成测量

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
    }

重点是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;
    }

从这里可以看出View的layoutParams是wrap_content的时候,getDefaultSize得到的是specSize,根据上边的图表分析知道specSize是父容器的剩余空间,这种情况和match_content的大小一样,所以我们在自定义View的时候需要处理specMode是AT_MOST的情况。

ViewGroup的measure过程

对于ViewGroup除了完成自身的测量还要完成子元素的测量,ViewGroup没有onMeasure方法但是它有一个meausreChildren方法

  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);
            }
        }
    }

只要子元素是可见的都进行测量,我们看一下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);
    }

measureChild的思想就是根据子元素的LayoutParams,然后再通过getChildMeasureSpec计算出子元素的MeasureSpec,将这个MeasureSpec传递给子元素的measure方法。

至此View,ViewGroup,DecorView的meausure都介绍完了。

你可能感兴趣的:(View的工作原理-Measure)