setContentView 那篇博文当中讲解了,addView 方法最终会调用 performTraversals()
方法中的 performXXX 去执行测量、布局和绘制工作,我们先从测量开始讲起。
测量工作是从 RootViewImpl 对象的 performTraversals()
开始的,该方法中的 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec)
开始测试的工作。
View mView;
private void performMeasure(int childWidthMeasureSpec, int childHeightMeasureSpec) {
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "measure");
try {
mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_VIEW);
}
}
它最终会调用 mView 的 measure 方法,这里的 mView 实际上是 DecorView,所以我们知道,测量工作是从最顶层开始执行的。
我们又知道,DecorView 实际上是 FrameLayout 的子类,但是 FrameLayout 当中又没有 measure 方法,其父类 ViewGroup 同样没有,实际上,调用的是 View 类的 measure 方法,在 measure 方法当中会调用 onMeasure 方法:
public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
...
if (forceLayout || needsLayout) {
...
if (cacheIndex < 0 || sIgnoreMeasureCache) {
// measure ourselves, this should set the measured dimension flag back
onMeasure(widthMeasureSpec, heightMeasureSpec);
...
} else {
...
}
...
}
...
}
这个 onMeasure 实际上调用的是 DecorView 当中的 onMeasure:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
...
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
...
}
在这个方法里调用了其父类的 onMeasure 方法,也就是调用了 FrameLayout 的 onMeasure 方法,这也就引入了 ViewGroup 的测量。
在讲解测量之前,需要先了解两个知识点,一个是 LayoutParams,一个是 MeasureSpec。
LayoutParams 是子元素用来告诉父元素其意图的工具,每个 LayoutParams 都封装了该 View 对于高度/宽度的要求,这些要求可以是以下之一:
FILL_PARENT
(API LV8 之后改为 MATCH_PARENT
): 表示子 View 想要和父视图(减去 padding)一样大。WRAP_CONTENT
:表示该 View 想要足够包含其内容(及 padding)的大小。我们在创建布局/控件时候为 adnroid:layout_width
和 android:layout_height
设置的属性值,就封装在 LayoutParams 当中。
通过调用 View 的 getLayoutParams()
方法,可以获取到封装的 LayoutParams。
每个 MeasureSpec 都封装了父布局对子元素的布局要求,当中都封装了一组宽度或高度的要求,MeasureSpec 由模式和尺寸组成。
通过调用 getSize(int measureSpec)
来获取封装的尺寸,调用 getMode(int easureSpec)
来获取封装的模式,共有三种模式:
ViewGroup 的测量本质是测量其子元素的大小,从而确定该 ViewGroup 的大小。
我们可以通过 getChildCount()
方法获取到该 ViewGroup 下子元素的个数,然后通过调用 getChildAt(int index)
可以获取 ViewGroup 下的子元素,然后对该元素做出测量。循环遍历整个 ViewGroup 就可以得到它内部所有子元素的大小,然后根据子元素的排列不同自然也就可以得到 ViewGroup 的大小了。
Android 内部封装了一些方法供我们来测量子 View,他们分别是:
我们分别来看一下这几个方法:
measureChild
protected void measureChild(View child, int parentWidthMeasureSpec,
int parentHeightMeasureSpec) {
final LayoutParams lp = child.getLayoutParams(); //获取子View的LayoutParams
// 获取父布局对子元素的宽/高测量限制,限制中包含了 padding 值
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
mPaddingLeft + mPaddingRight, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
mPaddingTop + mPaddingBottom, lp.height);
// 调用子元素的 measure 方法,开始测量子元素
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
measureChildWithMargins
protected void measureChildWithMargins(View child,
int parentWidthMeasureSpec, int widthUsed,
int parentHeightMeasureSpec, int heightUsed) {
//获取子View的LayoutParams
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
// 获取父布局对子元素的宽/高测量限制,限制中包含了 padding 和 margin 值
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);
// 调用子元素的 measure 方法,开始测量子元素
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
measureChildren
protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
final int size = mChildrenCount;
final View[] children = mChildren;
//没啥好说的了吧,循环遍历ViewGroup,分别测量其子元素
for (int i = 0; i < size; ++i) {
final View child = children[i];
if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
measureChild(child, widthMeasureSpec, heightMeasureSpec);
}
}
}
以 FrameLayout 为例,我们看一下 FrameLayout 的测量过程:
// 一个 lsit 用于存储宽/高设置为 match_parent 的子元素
private final ArrayList<View> mMatchParentChildren = new ArrayList<>(1);
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount(); //获取FrameLayout布局内子元素个数
// 父布局对该FrameLayout的宽高限制是否为 EXACTLY
final boolean measureMatchParentChildren =
MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||
MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
//保证mMatchParentChildren为空。
mMatchParentChildren.clear();
//用于存储FrameLayout的最大宽和高
int maxHeight = 0;
int maxWidth = 0;
int childState = 0;
// 循环遍历子元素,进行测量。
for (int i = 0; i < count; i++) {
final View child = getChildAt(i); //获取子元素
if (mMeasureAllChildren || child.getVisibility() != GONE) {
//开始测量子元素
measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
//获取子元素 LayoutParams
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
//分别所有子元素中(宽和高+margin)最大值
maxWidth = Math.max(maxWidth,
child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
maxHeight = Math.max(maxHeight,
child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
childState = combineMeasuredStates(childState, child.getMeasuredState());
if (measureMatchParentChildren) {
if (lp.width == LayoutParams.MATCH_PARENT ||
lp.height == LayoutParams.MATCH_PARENT) {
mMatchParentChildren.add(child);
}
}
}
}
//再根据padding、背景、等因素获取最终最大宽和高
maxWidth += getPaddingLeftWithForeground() + getPaddingRightWithForeground();
maxHeight += getPaddingTopWithForeground() + getPaddingBottomWithForeground();
maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
final Drawable drawable = getForeground();
if (drawable != null) {
maxHeight = Math.max(maxHeight, drawable.getMinimumHeight());
maxWidth = Math.max(maxWidth, drawable.getMinimumWidth());
}
//存储测量结果
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
resolveSizeAndState(maxHeight, heightMeasureSpec,
childState << MEASURED_HEIGHT_STATE_SHIFT));
//获取设置为 match_parent 的子元素个数
count = mMatchParentChildren.size();
if (count > 1) {
//循环遍历设置为 match_parent 的子元素
for (int i = 0; i < count; i++) {
final View child = mMatchParentChildren.get(i);
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
//下面的代码的主要目的是获取子组件的最大宽和高
final int childWidthMeasureSpec;
if (lp.width == LayoutParams.MATCH_PARENT) {
final int width = Math.max(0, getMeasuredWidth()
- getPaddingLeftWithForeground() - getPaddingRightWithForeground()
- lp.leftMargin - lp.rightMargin);
childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
width, MeasureSpec.EXACTLY);
} else {
childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
getPaddingLeftWithForeground() + getPaddingRightWithForeground() +
lp.leftMargin + lp.rightMargin,
lp.width);
}
final int childHeightMeasureSpec;
if (lp.height == LayoutParams.MATCH_PARENT) {
final int height = Math.max(0, getMeasuredHeight()
- getPaddingTopWithForeground() - getPaddingBottomWithForeground()
- lp.topMargin - lp.bottomMargin);
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
height, MeasureSpec.EXACTLY);
} else {
childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
getPaddingTopWithForeground() + getPaddingBottomWithForeground() +
lp.topMargin + lp.bottomMargin,
lp.height);
}
//针对这部分子元素,重新测量它们
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
}
}
看到这里,是不是对于 ViewGroup 的测量稍微有一些了解了,测量 ViewGroup 的本质,实际上就是遍历测试其内部子元素,根据子元素的排列,最终得到该 ViewGroup 的最终大小。
所以其重中之重,还是 View 的测量。
讲解 View 的测量之前,先需要声明一点,在测量工作结束后,一定要调用
setMeasuredDimension
方法将测量结果保存起来,否则会抛出异常!!!
在 FrameLayout 当中,通过调用 measureChildWithMargins
来进行子元素的测量,我们看一下这个方法:
protected void measureChildWithMargins(View child,
int parentWidthMeasureSpec, int widthUsed,
int parentHeightMeasureSpec, int heightUsed) {
//先获取子View的 LayoutParams
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
//获取该 FrameLayout 对子View 的测量要求和尺寸
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);
//正式开始调用View的measure方法进行测量,最终会调用View的onMeasure方法
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
我们看一下 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);
}
代码不加注释了,实质就是根据父 ViewGroup 对于该 FrameLayout 的 MeasureSpec 以及自身的 LayoutParams 来获取到该 FrameLayout 对于其子元素的 MeasureSpec。
然后在 measureChildWithMargins 方法中,将该 MeasureSpec 作为参数,开始对子元素的测量。
看 View 的 onMeasure 方法:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
可以看到,该方法要设置 View 的宽和高,看一下 getDefaultSize 方法:
public static int getDefaultSize(int size, int measureSpec) {
int result = size;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
switch (specMode) {
//如果父布局的测量限制模式为 UNSPECIFIED,则直接getSuggestedMinimumWidth()尺寸设置为该View的尺寸
case MeasureSpec.UNSPECIFIED:
result = size;
break;
case MeasureSpec.AT_MOST: //AT_MOST 不做处理
case MeasureSpec.EXACTLY: //如果为 EXACTLY,那么父View 的限制尺寸则为该View的尺寸
result = specSize;
break;
}
return result;
}
getSuggestedMinimumWidth() 是用来干啥的呢?
protected int getSuggestedMinimumWidth() {
return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
}
如果没有设置背景,那么返回该 View 的最小值,如果设置了,返回 View 最小值和背景最小尺寸之间较大的那个。
至此,测量工作完成。