您的位置:首页 > 移动开发 > Android开发

Android之理解MeasureSpec、学习源码的用16进制形式保存常量、Padding和Margin的区别

2018-02-03 23:42 405 查看
借鉴自开发艺术

在测量过程中,系统会将View的LayoutParams根据父容器所施加的规则,转换成对应的MeasureSpec,然后再根据这个MeasureSpec来测量出View的宽高。这里只是测量View的宽高,不一定等于View的最终宽高。

MeasureSpec代表一个32位的int值,最高的2位代表SpecMode(测量模式),余下30位代表SpecSize(某种测量模式下的规格大小)。

源码中有许多标记位,都是用16进制保存的,并且进行一些列的按位与、按位或等运算。所以可能会对我们源码的阅读会进行阻碍,所以得尝试性地学习下。

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

/**
* Measure specification mode: The parent has not imposed any constraint
* on the child. It can be whatever size it wants.
*/
public static final int UNSPECIFIED = 0 << MODE_SHIFT;

/**
* 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.
*/
public static final int EXACTLY     = 1 << MODE_SHIFT;

/**
* Measure specification mode: The child can be as large as it wants up
* to the specified size.
*/
public static final int AT_MOST     = 2 << MODE_SHIFT;


0x3,这里的x就是16进制,所以这里0x3=3。而如果是0x23,就是2*16+3了。

0x3 << MODE_SHIFT就是3 << 30。int是32位的(二进制下)。MODE_MASK的值就是11(3的二进制表示)左移30位。就是11...后面30个0。另外的运算起来就简单了。

MeasureSpec通过SpecMode和SpecSize打包成一个int值来避免过多的对象内存分配。太厉害了!这在平时的开发中也完全应该借鉴!他这样做,肯定证明这个做法作用不小。

SpecMode有3种

1.UNSPECIFIED

父容器不对View有任何限制,要多大给多大,这种情况一般用于系统内部,表示一种测量的状态。

2.EXACTLY

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

3.AT_MOST

父容器指定了一个可用大小即SpecSize,View的大小不能大于这个值,具体是什么值要看不同View的具体实现。它对应于LayoutParams中的wrap_content。

MeasureSpec和LayoutParams的对应关系

MeasureSpec不是唯一由LayoutParams来决定的,而是LayoutParams和父容器一起决定的,从而进一步决定View的宽高。另外,对于DecorView和普通View来说,MeasureSpec的转换过程略有不同。

1.对于DecorView的MeasureSpec由窗口的尺寸和其自身的LayoutParams来共同确定。

2.对于普通View,其MeasureSpec由父容器的MeasureSpec和自身的LayoutParams来共同决定。

childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width);
childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);

这是ViewRootImpl中measureHierarchy方法中的一段代码

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.MATCH_PARENT:精确模式,大小就是窗口的大小

LayoutParams.WRAP_CONTENT:最大模式,大小不定,但是不能超过窗口的大小

固定大小:精确模式,大小为LayoutParams中指定的大小

这3条结论看makemeasureSpec的参数就可以看出来。

对于普通View来说,前面也说了,由父容器的MeasureSpec和自身的LayoutParams来共同决定。

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

上面是view group自身的measureChildWithMargins方法。

解读一下 child宽度的MeasureSpec=获取child的MeasureSpec(view group的宽度MeasureSpec,左边距+右边距+lp的外左边距(?)+lp的外右边距)

从getChildMeasureSpec的参数可以看出来,child的MeasureSpec由1.父view group的MeasureSpec 2.lp的参数3.padding



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

这里的padding就是margin+padding,就代表父控件已经占用的空间。int size = Math.max(0, specSize - padding);就是留给子view的可用空间。不能小于0。

下面的switch就是根据不同的情况,得出子view的MeasureSpec了。

(书上还有一张图,我这里觉得没啥必要记忆这东西,不予贴出了)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: