androidP: View的工作原理-理解MeasureSpec

参考链接
https://blog.csdn.net/carson_ho/article/details/94545178?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2aggregatepagefirst_rank_ecpm_v1~rank_v31_ecpm-7-94545178.pc_agg_new_rank&utm_term=android+measurespec.makemeasurespec&spm=1000.2123.3001.4430

一、MeasureSpec
MeasureSpec在很大程序上决定了一个View的尺寸规格,之所以说是很大程度上是因为这个过程还受父容器的影响,因为父容器影响VIew的MeasureSpec的创建过程。在测量过程中,系统会将View的LayoutParams根据父容器所施加的规则转换成对应的MeasureSpec,然后再根据这个measureSpec来测量出View的宽/高。这里的宽/高是测量的宽/高,不一定等于View的最终宽/高。

测量规格(MeasureSpec)是由测量模式(mode)和测量大小(size)组成,共32位(int类型),其中:
测量模式(mode):占测量规格(MeasureSpec)的高2位;
测量大小(size):占测量规格(MeasureSpec)的低30位。
在这里插入图片描述
下面看一下MeasureSpec的常量定义。MeasureSpec类是View类的内部类。

// android_build_9/frameworks/base/core/java/android/view/View.java
public static class MeasureSpec {
   // 进位大小 = 2的30次方
   // int的大小为32位,所以进位30位 = 使用int的32和31位做标志位
    private static final int MODE_SHIFT = 30;
    
    // 运算遮罩:0x3为16进制,10进制为3,二进制为11
    // 3向左进位30 = 11 00000000000(11后跟30个0)  
    // 作用:用1标注需要的值,0标注不要的值。因1与任何数做与运算都得任何数、0与任何数做与运算都得0
    private static final int MODE_MASK  = 0x3 << MODE_SHIFT;
    
    // UNSPECIFIED的模式设置:0向左进位30 = 00后跟30个0,即00 00000000000
    // 通过高2位
    public static final int UNSPECIFIED = 0 << MODE_SHIFT;
    
    // EXACTLY的模式设置:1向左进位30 = 01后跟30个0 ,即01 00000000000
    public static final int EXACTLY     = 1 << MODE_SHIFT;
    
    // AT_MOST的模式设置:2向左进位30 = 10后跟30个0,即10 00000000000
    public static final int AT_MOST     = 2 << MODE_SHIFT;
    
    //打包MeasureSpec
     public static int makeMeasureSpec(@IntRange(from = 0, to = (1 << MeasureSpec.MODE_SHIFT) - 1) int size,
                                          @MeasureSpecMode int mode) {
            if (sUseBrokenMakeMeasureSpec) {
                // measureSpec = size + mode;此为二进制的加法 而不是十进制
	            // 设计目的:使用一个32位的二进制数,其中:32和31位代表测量模式(mode)、后30位代表测量大小(size)
	            // 例如size=100(4),mode=AT_MOST,则measureSpec=100+10000...00=10000..00100 
                return size + mode;
            } else {
                return (size & ~MODE_MASK) | (mode & MODE_MASK);
            }
        }
  
  //解包MeasureSpec:获取mode
  @MeasureSpecMode
        public static int getMode(int measureSpec) {
            //noinspection ResourceType
            // 即:测量模式(mode) = measureSpec & MODE_MASK;  
            // MODE_MASK = 运算遮罩 = 11 00000000000(11后跟30个0)
            //原理:保留measureSpec的高2位(即测量模式)、使用0替换后30位
            // 例如10 00..00100 & 11 00..00(11后跟30个0) = 10 00..00(AT_MOST),这样就得到了mode的值
            return (measureSpec & MODE_MASK);
        }  
 //解包MeasureSpec:获取size
public static int getSize(int measureSpec) {
            // size = measureSpec & ~MODE_MASK;  
            // 原理类似上面,即 将MODE_MASK取反,也就是变成了00 111111(00后跟30个1),将32,31替换成0也就是去掉mode,
            //保留后30位的size  
            return (measureSpec & ~MODE_MASK);
        }    
}

MeasureSpec类用一个变量封装了测量模式(mode)和测量大小(size):通过使用二进制,将测量模式(mode)和测量大小(size)打包成一个int值,并提供了打包和解包的方法,这样的做法是为了减少对象内存分配和提高存取效率。

SpecMode有三类UNSPECIFIED、EXACTLY和AT_MOST。
**UNSPECIFIED:**父容器不对View有任何限制,要多大给多大,这种情况一般用于系统内部,表示一种测量的状态;
**EXACTLY:**父容器已经检测出View所需要的精确大小,这个时候View的最终大小就是SpecSize所指定的值。它对应于LayoutParams中的match_parent和具体的数值这两种情况;
**AT_MOST:**父容器指定了一个可用大小,即SpecSize,View的大小不能大于这个值,具体是什么值要看不同View的具体实现。它对应于LayoutParams中的wrap_content。

二、MeasureSpec和LayoutParams的对应关系
MeasureSpec不是唯一由LayoutParams决定的,LayoutParams需要和父容器一起才能决定View的MeasureSpec,从而进一步决定View的宽/高。另外,对于顶级View(DecorView)和普通View来说,MeasureSpec的转换过程略有不同。对于DecorView,其MeasureSpec由窗口的尺寸和其自身的LayoutParams来共同确定;对于普通的View,其MeasureSpec由父容器的MeasureSpec和自身的LayoutParams来共同确定。MeasureSpec一旦确定后,onMeasure中就可以确定View的测量宽/高。

1、对于DecorView来说,在ViewRootImpl中的measureHierarchy()方法中实现了DecorView的MeasureSpec的创建过程,其中desiredWindowWidth、desiredWindowHeight是屏幕尺寸。

// android_build_9/frameworks/base/core/java/android/view/ViewRootImpl.java
private boolean measureHierarchy(final View host, final WindowManager.LayoutParams lp,
            final Resources res, final int desiredWindowWidth, final int desiredWindowHeight) {
     int childWidthMeasureSpec;
     int childHeightMeasureSpec;
     ...
     childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
     childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
     performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
     ...
}

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创建过程遵守如下规则,根据它的LayoutParams中的宽/高参数来划分。
**LayoutParams.MATCH_PARENT:**精确模式,大小是窗口的大小;
**LayoutParams.WRAP_CONTENT:**最大模式,大小不定,但是不能超过窗口的大小;
**固定大小(比如100dp):**精确模式,大小为LayoutParams中指定的大小。

2、对于普通View来说,这里是指我们布局中的View,View的measure过程由ViewGroup传递而来,ViewGroupde measureChildWithMargins方法。

// android_build_9/frameworks/base/core/java/android/view/ViewGroup.java
/**
     * Ask one of the children of this view to measure itself, taking into
     * account both the MeasureSpec requirements for this view and its padding
     * and margins. The child must have MarginLayoutParams The heavy lifting is
     * done in getChildMeasureSpec.
     *
     * @param child The child to measure
     * @param parentWidthMeasureSpec The width requirements for this view
     * @param widthUsed Extra space that has been used up by the parent
     *        horizontally (possibly by other children of the parent)
     * @param parentHeightMeasureSpec The height requirements for this view
     * @param heightUsed Extra space that has been used up by the parent
     *        vertically (possibly by other children of the parent)
     */
 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);
    }

/**
     * Does the hard part of measureChildren: figuring out the MeasureSpec to
     * pass to a particular child. This method figures out the right MeasureSpec
     * for one dimension (height or width) of one child view.
     *
     * The goal is to combine information from our MeasureSpec with the
     * LayoutParams of the child to get the best possible results. For example,
     * if the this view knows its size (because its MeasureSpec has a mode of
     * EXACTLY), and the child has indicated in its LayoutParams that it wants
     * to be the same size as the parent, the parent should ask the child to
     * layout given an exact size.
     *
     * @param spec The requirements for this view
     * @param padding The padding of this view for the current dimension and
     *        margins, if applicable
     * @param childDimension How big the child wants to be in the current
     *        dimension
     * @return a MeasureSpec integer for the child
     */
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);
    }
 

measureChildWithMargins()方法会对子元素进行measure,在调用子元素的measure方法之前会先通过getChildMeasureSpec()方法来得到子元素的MeasureSpec。子元素的MeasureSpec的创建与付容器的MeasureSpec和子元素本身的LayoutParams有关,此外还和View的margin及padding有关。

getChildMeasureSpec()方法的主要作用是根据父容器的MeasureSpec(参数spec)同时结合View本身的LayoutParams(参数childDimension)来确定子元素的MeasureSpec,参数中的padding是指父容器中以占用的空间大小,因此子元素可用的大小为父容器的尺寸减去padding:

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

getChildMeasureSpec()方法以表格方式表示:
在这里插入图片描述
对于普通View,其MeasureSpec由父类容器的MeasureSpec和自身的LayoutParams来共同决定。当View采用固定宽/高的时候,不管不容器的MeasureSpec是什么,View的MeasureSpec都是精确模式并且大小遵循LayoutParams中的大小。当VIew的宽/高是match_parent时,如果父容器的模式是精确模式,那么View也是精确模式并且大小是父容器的剩余空间;如果父容器是最大模式,那么View也是最大模式并且大小不会超过父容器的剩余空间。当View的宽/高是wrap_content时,不管父容器的模式是精准还是最大化,View的模式总是最大化并且大小不能超过父容器的剩余空间。由于UNSPECIFIED模式适用于系统内部多次measure情况,很少用到,故此处不讨论。

**EXACTLY:**父容器已经检测出View所需要的精确大小,这个时候View的最终大小就是SpecSize所指定的值。它对应于LayoutParams中的match_parent和具体的数值这两种情况;
当布局文件中设置的为具体数值时
1)小于父容器最大数值:测量值为布局文件设置的值。
2)大于父容器最大数值:测量值为父容器最大数值。
3)设置为wrap_content: 测量值为父容器最大数值。
4)设置为match_parent:类型均为EXACTLY类型,测量值为测量值为父容器最大数值。

**AT_MOST:**父容器指定了一个可用大小,即SpecSize,View的大小不能大于这个值,具体是什么值要看不同View的具体实现。它对应于LayoutParams中的wrap_content。
1)小于父容器最大值:测量值为父容器最大值。
2)大于父容器最大数值:测量值为父容器最大数值。
3)设置为wrap_content: 测量值为父容器最大数值。

下一节,View的工作流程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值