0%

Android控件总结2

上一篇中,分析了整体上的流程,这里来分析下细节性的问题。

  1. onMeasure()中的 MeasureSpec 是怎么来的?
    在之前的流程中,通过requestLayout() 触发 performTravsals(),在 performTravsals() 里面会可能多次调用 performMeasure(...),而 performMeasure(...) 会调用mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);进入到view和它的子控件的层层调用中,这里的mView在Activity等中就是指DecoreView,在自己创建的window中就是setView()传的view,这里就是最初的源头了。
    performMeasure中传入的参数来源 :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    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;
    }
    这里的 windowSize 是指wms计算后返回的window的大小,而rootDimension则是相应的window的windowManager.Layoutparam,(未验证)值是ViewGroup.LayoutParams.MATCH_PARENT(未验证),对于Activity来说,这里得到的最初的参数应该是size = 分辨率大小减去状态栏,导航栏),mode = MeasureSpec.EXACTLY。在ViewGroup中为子控件生成measureSpec的默认方法:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    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的 onMeasure()方法,可以看到,生成的值是受到父控件和子控件共同控制的。因此 MeasureSpec 最初是屏幕的分辨率,而后传给子控件的过程中会根据 父控件和子控件 的参数发生变化。