View的工作原理_理解MeasureSpec

MeasureSpec概要:
MeasureSpec在很大程度上决定了一个View的尺寸规格,之所以是很大程度上是因为这个过程还要受到父容器的影响.在测量的过程中,系统会将view的LayoutParams根据父容器所施加的规则转换成对应的MeasureSpec,然后在根据这个MeasureSpec来测量出View的宽高,具体看最下面的源码中的getChildMeasureSpec()方法(Measure:/'meʒə/)

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

说明:
1,&和&&的区别:
&:不管条件是否满足,都会执行"&“符号左右两边的程序例如:if(a>5&b<2) 不管a>5是否为真,都会执行b<2
&&:只有当”&&“左边条件满足的情况下,才会执行”&&"右边的程序 :if(a>5&&b<2) 只有a>5为真,才会会执行b<2

2,左移 1<<2 表示的是 1左移2位 0001 -->0100
3,取反 “~” ~5=-6, ~9=-10 ~a=-(a+1) (这里涉及到的是补码,源码,的关系,具体可自己百度)

MeasureSpec具体说明:

MeasureSpec代表一个32位的int值,高2位代表SpecMode,低30位代表SpecSize,SpecMode指的是测量模式,SpecSize指的是在某种测量模式下的大小.

SpecMode:有3类

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

AT_MOST:父容器指定了一个最大的值,子view的最终大小不能超过它,它对应于LayoutParams中的wrap_content.

UNSPECIFIED:父容器对view的大小没有任何限制,子view想多大就给多大,一般用于系统内部

MeasureSpec和LayoutParams的对应关系

普通的View:
其MeasureSpec由父容器的MeasureSpec和自身的LayoutParams来共同决定,MeasureSpec一旦确定后,onMeasure中就可以确定View的测量宽高.

顶级View(DecorView):
其MeasureSpec由窗口的尺寸和自身的LayoutParams来共同决定

对于普通的View来讲(这里是指我们xml布局中的View),View的measure过程由ViewGroup传递而来

源码: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);
    }

由以上源码可知:通过getChildMeasureSpec()方法 得到孩子的childWidthMeasureSpec,而getChildMeasureSpec()方法所需要传递的参数是 父元素的parentWidthMeasureSpec,和子元素LayoutParams相关的参数,由此可以得出的结论是:子元素的MeasureSpec的创建与父容器的MeasureSpec和子元素的LayoutParams有关(此外还和View的margin和padding有关)

源码: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);
    }

getChildMeasureSpec()方法清楚的展示了普通的View的MeasureSpec的创建规则,具体表格对应关系如下:
(注意:表中的parentSize是指父容器中目前可使用的大小)

View的工作原理_理解MeasureSpec_第1张图片

你可能感兴趣的:(Android基础总结)