Android View的Measure测量流程全解析

173 阅读3分钟

摘自 juejin.cn/post/684490…

问:一个FrameLayout设置wrap_content,套一个button(只有这一个子view),最终父子view的大小?为什么?

View是通过onMeasure()来确定自己的宽高的,(ViewGroup是个抽象类继承自View,它并没有重写onMeasure(),所以如果自定义ViewGroup的时候没有重写onMeasure(),它最终调的还是View的onMeasure()方法)

View.onMeasure() -> setMeasuredDimension()->setMeasuredDimensionRaw()

setMeasuredDimension(int measuredWidth, int measuredHeight)可以设置View的宽高,参数是怎么来的

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

在没有重写onMeasure()方法的情况下,MeasureSpec就决定了View/ViewGroup的宽高。

MeasureSpec包含两部分,一个是SpecMode(测量模式),一个是SpecSize(测量大小)。它是用一个32位的int值来表示的,高2位代表测量模式,低30位代表测量大小。

模式说明
EXACTLY精确的宽高。width、height设置了具体值或者match_parent
AT_MOSTwidth、height设置为wrap_content
UNSPECIFIED父容器不对View有任何限制,一般用于系统

MeasureSpec是如何生成的

不重写onMeasure()的情况下,MeasureSpec就决定了这个View/ViewGroup的宽高,这个MeasureSpec显然是这个View/ViewGroup的父容器在调用子View的measure()方法时传进来的,也就是说一个View/ViewGroup的MeasureSpec是由其父容器生成的,那么是怎么生成的呢?里面的SpecSize和SpecMode是由什么决定的呢?

父容器通过调ViewGroup中的getChildMeasureSpec()来生成子View的MeasureSpec。getChildMeasureSpec()中主要是通过父容器的MeasureSpec以及子Views设置的宽高来共同决定子View的MeasureSpec中的SpecMode和SpecSize。

View.measure() -> ViewGroup.onMeasure()-> ViewGroup.getChildMeasureSpec()

getChildMeasureSpec()代码里的生成规则:

1.当子View的宽高设置的是具体数值时,显然可以直接拿到子View的宽高,则子View宽高就确定了,不用再去考虑父容器的SpecMode了,此时子View的SpecMode为EXACTLY,SpecSize就是设置的宽高。

2.当子View的宽高设置的是match_parent, 则不管父容器的SpecMode是什么模式,子View的SpecSize就等于父容器的宽高,而子View的SpecMode随父容器的SpecMode。(这里没有考虑UNSPECIFIED模式,如果父容器是UNSPECIFIED模式,则子View SpecSize为0,SpecMode为UNSPECIFIED)(都随父)

3.当子View的宽高设置的是wrap_content,因为这种情况父容器实在不知道子View应该多宽多高,所以子View的SpecSize给的是父容器的宽高,也就是说只是给子View限制了一个最大宽高,而子View的SpecMode是AT_MOST模式。(这里没有考虑UNSPECIFIED模式,如果父容器是UNSPECIFIED模式,则子View SpecSize为0,SpecMode为UNSPECIFIED)。

    通过上面的解析我们可以知道,当你给一个View/ViewGroup设置宽高为具体数值或者match_parent,它都能正确的显示,但是如果你设置的是wrap_content,则默认显示出来是其父容器的大小,如果你想要它正常的显示为wrap_content,则你就要自己重写onMeasure()来自己计算它的宽高度并设置。所以我们平常自定义View/ViewGroup的时候之所以要重写onMeasure(),就是为了能让wrap_content达到效果。

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

回到问题,button其实是有具体值的,父容器FrameLayout wrap_content,所以最终子View的测量模式是EXACTLY,大小是自己的默认的大小 圈中上面那个.png