美文网首页
了解MeasureSpec

了解MeasureSpec

作者: 空山Echo | 来源:发表于2019-05-09 13:37 被阅读0次

    MeasureSpec

    • MeasureSpec封装了从父级传递给子级的布局要求。每个MeasureSpec代表宽度或高度的要求。
    • MeasureSpecs实现为32位的INT,由大小和模式组成,高2位代表模式,低30位代表尺寸,它通过将模式和大小打包成一个INT值来减少对象内存分配,并提供打包和解包的方法。
    • 模式分类:

    UNSPECIFIED:父容器不对视图有任何限制,给它想要的任何尺寸一般用于系统内部,表示一种测量状态。
    EXACTLY:父容器已经检测出视图的精确大小,这时候视图的大小就是所指定的值。它对应于的LayoutParams中的match_parent和具体数值两种模式。
    AT_MOST:父容器指定了一个可用大小,子视图的大小不能大于这个值,具体值要看VEW的实现,它对应于的LayoutParams中的WRAP_CONTENT。

        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 {}
    
            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);
                }
            }
    
            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);
            }
    
            static int adjust(int measureSpec, int delta) {
                final int mode = getMode(measureSpec);
                int size = getSize(measureSpec);
                if (mode == UNSPECIFIED) {
                    // No need to adjust size for UNSPECIFIED mode.
                    return makeMeasureSpec(size, UNSPECIFIED);
                }
                size += delta;
                if (size < 0) {
                    Log.e(VIEW_LOG_TAG, "MeasureSpec.adjust: new size would be negative! (" + size +
                            ") spec: " + toString(measureSpec) + " delta: " + delta);
                    size = 0;
                }
                return makeMeasureSpec(size, mode);
            }
        }
    

    MeasureSpec与的LayoutParams的关系

    普通视图的测量参数是由父容器传递而来,这里我们可以看下的ViewGroup的measureChildWithMargins方法:

        /**
        * @param child 要测量的子view
        * @param parentWidthMeasureSpec 该容器对子view的宽度要求
        * @param widthUsed 父级水平使用的额外空间(可能是父级的其他子级),可以理解为已经被占用的空间
        * @param parentHeightMeasureSpec 该容器对子view的高度要求
        * @param heightUsed 父级垂直使用的额外空间(可能是父级的其他子级)
        */
        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);
        }
    
    可以看到子视图的测量参数是由getChildMeasureSpec方法得到的,它和父容器(即该ViewGroup中)对子图的测量要求,父容器的pandding,子视图的余量,父容器的已占空间都由关系
        /**
        * @param spec 该容器的测量要求
        * @param padding 可以简单理解为子view和该容器之间的间隔,由多个值决定
        * @param childDimension 子view的期望值,即子view的LayoutParams的宽高
        */
        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的LayoutParams转换生成一个MeasureSpec上述代码可以简单翻译成下面这个图表:


    image.png

    MeasureSpec.EXACTLY

    官方文档
    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.
    父容器已经检测出子View所需要的精确大小.该子View最终的测量大小即为SpecSize.

    1. 当子View的LayoutParams的宽(高)采用具体的值(如100px)时且父容器的MeasureSpec为 MeasureSpec.EXACTLY或者 MeasureSpec.AT_MOST或者MeasureSpec.UNSPECIFIED时:

    系统返回给该子View的specMode就为MeasureSpec.EXACTLY
    系统返回给该子View的specSize就为子View自己指定的大小(childSize)

    通俗理解:(子view明确大小dp/px,不论父viewMeasureSpec,子view就是指定大小)

    • 子View的LayoutParams的宽(高)采用具体的值(如100px)时,那么说明该子View的大小明确到已经用具体px值 指定的地步了,那么此时不管父容器的specMode是什么,系统返回给该子View的specMode总是MeasureSpec.EXACTLY,并且 系统返回给该子View的specSize就为子View自己指定的大小(childSize).
    1. 当子View的LayoutParams的宽(高)采用match_parent时并且父容器的MeasureSpec为 MeasureSpec.EXACTLY时:

    系统返回给该子View的specMode就为 MeasureSpec.EXACTLY
    系统返回给该子View的specSize就为该父容器剩余空间的大小(parentLeftSize)

    通俗地理解:
    子View的LayoutParams的宽(高)采用match_parent时并且父容器的MeasureSpec为 MeasureSpec.EXACTLY。这时候说明子View的大小还是挺明确的:就是要和父容器一样大,更加直白地说就是父容器要怎样子View就要怎样

    • 所以,如果父容器MeasureSpec为 MeasureSpec.EXACTLY那么:
      系统返回给该子View的specMode就为 MeasureSpec.EXACTLY,和父容器一样.
      系统返回给该子View的specSize就为该父容器剩余空间的大小(parentLeftSize),就是父容器的剩余大小
    • 同样的道理如果此时,MeasureSpec为 MeasureSpec.AT_MOST呢?
      系统返回给该子View的specMode也为 MeasureSpec.AT_MOST,和父容器一样.
      系统返回给该子View的specSize也为该父容器剩余空间的大小(parentLeftSize),就是父容器的剩余大小.

    MeasureSpec.AT_MOST

    官方文档
    The child can be as large as it wants up to the specified size.
    父容器指定了一个可用大小即specSize,子View的大小不能超过该值.

    1. 当子View的LayoutParams的宽(高)采用match_parent时并且父容器的MeasureSpec为 MeasureSpec.AT_MOST时:

    系统返回给该子View的specMode就为 MeasureSpec.AT_MOST
    系统返回给该子View的specSize就为该父容器剩余空间的大小(parentLeftSize)

    这种情况已经在上面介绍 MeasureSpec.EXACTLY时(2)已经讨论过了.

    1. 当子View的LayoutParams的宽(高)采用wrap_content时并且父容器的MeasureSpec为 MeasureSpec.EXACTLY时:

    系统返回给该子View的specMode就为 MeasureSpec.AT_MOST
    系统返回给该子View的specSize就为该父容器剩余空间的大小(parentLeftSize)

    通俗地理解: (子view为wrap_content,不论父view,子view为AT_MOST)

    • 子View的LayoutParams的宽(高)采用wrap_content时说明这个子View的宽高不明确,要视content而定. 这个时候如果父容器的MeasureSpec为 MeasureSpec.EXACTLY即父容器是一个精确模式;这个时候简单地说 ,子View是不确定的,父容器是确定的,那么 :
      系统返回给该子View的specMode也就是不确定的即为 MeasureSpec.AT_MOST
      系统返回给该子View的specSize就为该父容器剩余空间的大小(parentLeftSize)
    1. 当子View的LayoutParams的宽(高)采用wrap_content时并且父容器的MeasureSpec为 MeasureSpec.AT_MOST时:

    系统返回给该子View的specMode就为 MeasureSpec.AT_MOST
    系统返回给该子View的specSize就为该父容器剩余空间的大小(parentLeftSize)

    通俗地理解: (子view为wrap_content,不论父view,子view为AT_MOST)

    • 子View的LayoutParams的宽(高)采用wrap_content时说明这个子View的宽高不明确,要视content而定. 这个时候如果父容器的MeasureSpec为 MeasureSpec.AT_MOST。这个时候简单地说 子View是不确定的,父容器也是不确定的,那么:
      系统返回给该子View的specMode也就是不确定的即为 MeasureSpec.AT_MOST
      系统返回给该子View的specSize就为该父容器剩余空间的大小(parentLeftSize)

    MeasureSpec.UNSPECIFIED

    官方文档
    The parent has not imposed any constraint on the child. It can be whatever size it wants.
    父容器不对子View的大小做限制.
    一般用作Android系统内部,或者ListView和ScrollView.在此不做讨论.

    简述MeasureSpec的使用

    • MeasureSpec字面意思可以理解为“测量规格”

    • 在测量过程中,系统会将View的LayoutParams根据父容器的规格转换生成相应的MeasureSpec,然后根据这个MeasureSpec测量出视图的尺寸。也就是说,view的MeasureSpec是由自己的LayoutParams和父容器所给的MeasureSpec共同决定的

    • OnMeasure(int widthMeasureSpec, int heightMeasureSpec)该方法就是我们自定义控件中测量逻辑的方法,该方法中的参数是父view传递给子view测量width与height大小的要求。在我们自定义视图中,要做的就是根据widthMeasureSpec与heightMeasureSpec进行对view宽高的一个测量。

    作者:风少侠
    链接:https://www.jianshu.com/p/7e48efe54f2c
    来源:简书
    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

    相关文章

      网友评论

          本文标题:了解MeasureSpec

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