美文网首页
View之测量MeasureSpec

View之测量MeasureSpec

作者: 钦_79f7 | 来源:发表于2019-12-17 12:24 被阅读0次

    View中的MeasureSpec

    View在测量过程中依赖于测量规则 MeasureSpec

    • DecorView 的MeasureSpec 取决于 Window的尺寸与 自身的LayoutParams
    • 普通View的 MeasureSpec 取决于 父容器的尺寸 与自身的LayoutParams

    MeasureSpec

    View 中的静态内部类 MeasureSpec

        public static class MeasureSpec {
            private static final int MODE_SHIFT = 30;
            private static final int MODE_MASK  = 0x3 << MODE_SHIFT;
            
            public static final int UNSPECIFIED = 0 << MODE_SHIFT;
    
            public static final int EXACTLY     = 1 << MODE_SHIFT;
    
            public static final int AT_MOST     = 2 << MODE_SHIFT;
    
            public static int makeMeasureSpec(@IntRange(from = 0, to = (1 << MeasureSpec.MODE_SHIFT) - 1) int size,
                                              @MeasureSpecMode int mode) {
                if (sUseBrokenMakeMeasureSpec) {
                    return size + mode;
                } else {
                    return (size & ~MODE_MASK) | (mode & MODE_MASK);
                }
            }
    
            @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。
    SpecMode:测量模式;SpecSize:代表某种测量模式下的尺寸大小。

    MeasureSpec通过将SpecMode 与 SpecSize打包成一个int值来避免过多的对象内存分配,为了方便操作提供的了 打包与解包的方法。

    • makeMeasureSpec:打包
    • getSize、getMode:解包

    SpecMode

    • UNSPECIFIED

      不指定测量模式。父容器不对View有任何限制,要多大有多大,这种情况一般用于系统内部,表示一种测量状态。开发过程中,几乎用不到。

    • EXACTLY

      精确值模式。对应于LayoutParams中的 match_parent 与 具体的数值。父容器已经检测出View所需要的精确大小,这个时候View的最终大小就是SpecSize所指定的值。

    • AT_MOST

      最大值模式。对应于LayoutParams中 wrap_content。父容器指定了一个可用的大小即SpecSize,View的大小不能大于这个值,具体是什么值要看不同View的具体实现。

    MeasureSpec 与 LayoutParams 的关系

    DecorView

    ViewRootImpl#measureHierarchy

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

    其中desiredWindowWidth,desiredWindowHeight是屏幕的尺寸。

    ViewRootImpl#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的LayoutParams的宽高参数来划分。

    • LayoutParams.MATCH_PARENT:精确模式,大小就是窗口的大小;
    • LayoutParams.WRAP_CONTENT: 最大模式,大小不定,但是不能超过窗口的大小;
    • 固定大小:精确模式,大小为LayoutParams中指定的大小。

    普通View

    这里的普通View指的是布局中View

    ViewGroup#measureChildWithMaigin

        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,在此之前则会先通过getChildMeasureSpec 得到子元素的 MeasureSpec。很显然这里子元素的MeasureSpec与父容器的MeasureSpec以及自身的LayoutParams有关,此外还与View的margin,padding有关。

    关于 getChildMeasureSpec 的源码在下面贴出,这里先写出分析结果。

    其代码逻辑主要就是根据父容器的MeasureSpec 同时结合自身的LayoutParams来确定子元素的MeasureSpec,参数中的padding是指父容器中已占用的空间大小,因此子元素可用的大小是父容器的尺寸减去padding,如下代码:

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

    这里根据getChildMeasureSpec 的代码做出一个表:

    普通View的MeasureSpec创建规则

    childLayoutParams\parentLayoutParams EXACTLY AT_MOST UNSPECIFIED
    dp/px EXACTLY
    childSize
    EXACTLY
    childSize
    EXACTLY
    childSize
    match_parent EXACTLY
    parentSize
    AT_MOST
    parentSize
    UNSPECIFIED
    0
    wrap_content AT_MOST
    parentSize
    AT_MOST
    parentSize
    UNSPECIFIED
    0

    把代码逻辑简化成这个表之后,就显得很清晰了。

    • View 固定宽高时,不管父容器的MeasureSpec 是什么,View 的MeasureSpec 都是精确模式,并且其大小取决于LayoutParams 中的大小
    • View 是match_parent 时,其MeasureSpec与父容器保持一致;精确模式,则尺寸与父容器的剩余空间一致;最大模式,则尺寸最大不能超过父容器的剩余空间;
    • View 是wrap_content 时,不管父容器的MeasureSpec,View的MeasureSpec都是最大模式,且最大不能超过父容器的剩余空间。
    • UNSPECIFIED 模式,不需要关注此模式,开发中几乎用不到

    源码如下:

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

    相关文章

      网友评论

          本文标题:View之测量MeasureSpec

          本文链接:https://www.haomeiwen.com/subject/lajsnctx.html