AndroidView绘制流程三(测量)
作者:互联网
在上一篇文章中,我们分析了View的绘制流程,在ViewRootImpl类performTraversals()方法中,依次执行performMeasure()、performLayout()以及performDraw()方法,下面我们来看一下这三个方法都做了哪些事情。
1)performMeasure()
private void performMeasure(int childWidthMeasureSpec, int childHeightMeasureSpec) {
if (mView == null) {
return;
}
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "measure");
try {
mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_VIEW);
}
}
调用了mView.measure()方法,这里的mView就是传递的DecorView,进入measure()方法
public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
//...
//...
//...
// Suppress sign extension for the low bytes
long key = (long) widthMeasureSpec << 32 | (long) heightMeasureSpec & 0xffffffffL;
//View的缓存策略
if (mMeasureCache == null) mMeasureCache = new LongSparseLongArray(2);
final boolean forceLayout = (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
final boolean specChanged = widthMeasureSpec != mOldWidthMeasureSpec
|| heightMeasureSpec != mOldHeightMeasureSpec;
final boolean isSpecExactly = MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY
&& MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY;
final boolean matchesSpecSize = getMeasuredWidth() == MeasureSpec.getSize(widthMeasureSpec)
&& getMeasuredHeight() == MeasureSpec.getSize(heightMeasureSpec);
final boolean needsLayout = specChanged
&& (sAlwaysRemeasureExactly || !isSpecExactly || !matchesSpecSize);
if (forceLayout || needsLayout) {
// first clears the measured dimension flag
mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
resolveRtlPropertiesIfNeeded();
//这里表示从缓存中获取
int cacheIndex = forceLayout ? -1 : mMeasureCache.indexOfKey(key);
if (cacheIndex < 0 || sIgnoreMeasureCache) {
//缓存中没取到
// measure ourselves, this should set the measured dimension flag back
onMeasure(widthMeasureSpec, heightMeasureSpec);
mPrivateFlags3 &= ~PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT;
} else {
//缓存中存在
long value = mMeasureCache.valueAt(cacheIndex);
// Casting a long to int drops the high 32 bits, no mask needed
setMeasuredDimensionRaw((int) (value >> 32), (int) value);
mPrivateFlags3 |= PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT;
}
mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
}
//添加至缓存中
mMeasureCache.put(key, ((long) mMeasuredWidth) << 32 |
(long) mMeasuredHeight & 0xffffffffL); // suppress sign extension
}
在该方法中,mMeasureCache对象,是一个View的缓存策略,缓存中没取到则调用onMeasure()方法,反之调用setMeasuredDimensionRaw()方法,最后将数据放入缓存mMeasureCache对象中。这里我们进入onMeasure()方法,发现其调用了setMeasuredDimension(),紧接着调用了setMeasuredDimensionRaw(),在该方法中做了两件事,给成员变量mMeasuredWidth、mMeasuredHeight赋值,并重置标记位
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
//...
//...
setMeasuredDimensionRaw(measuredWidth, measuredHeight);
}
private void setMeasuredDimensionRaw(int measuredWidth, int measuredHeight) {
//赋值
mMeasuredWidth = measuredWidth;
mMeasuredHeight = measuredHeight;
//重置标记位
mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
}
至此View的测量完成。
那么setMeasuredDimensionRaw(int measuredWidth, int measuredHeight)方法中,传递的两个参数是如何计算的呢,回到最开始ViewRootImpl的performTraversals()方法中,我们发现传递的参数measuredWidth、measuredHeight是通过getRootMeasureSpec()方法进行计算的。
private void performTraversals() {
if (!mStopped || mReportNextDraw) {
boolean focusChangedDueToTouchMode = ensureTouchModeLocally(
(relayoutResult & WindowManagerGlobal.RELAYOUT_RES_IN_TOUCH_MODE) != 0);
if (focusChangedDueToTouchMode || mWidth != host.getMeasuredWidth()
|| mHeight != host.getMeasuredHeight() || contentInsetsChanged ||
updatedConfiguration) {
int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
}
}
}
进入该方法,这里传递的windowSize就是window的大小,rootDimension就是传递的DecorView的大小
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;
}
这里我们发现主要调用了MeasureSpec.makeMeasureSpec(),在该方法中,传递了两个参数windowSize, MeasureSpec.EXACTLY,那么View的测绘就是 模式+尺寸 返回的MeasureSpec 值,找到MeasureSpec静态类,发现MeasureSpec是一个32位的int值(00000000000000000000000000000000)
MeasureSpec = SpecMode(前2位) + SpecSize(后30位)
在该类中定义了
//MODE_SHIFT 30位
private static final int MODE_SHIFT = 30;
private static final int MODE_MASK = 0x3 << MODE_SHIFT;
//UNSPECIFIED 表示0左移30位
public static final int UNSPECIFIED = 0 << MODE_SHIFT;
//EXACTLY 表示1左移30位
public static final int EXACTLY = 1 << MODE_SHIFT;
//AT_MOST表示2左移30位
public static final int AT_MOST = 2 << MODE_SHIFT;
UNSPECIFIED对应的值就是00000000000000000000000000000000
EXACTLY对应的值就是01000000000000000000000000000000
AT_MOST对应的值就是10000000000000000000000000000000
进入MeasureSpec类中makeMeasureSpec()方法
public static int makeMeasureSpec(@IntRange(from = 0, to = (1 << MeasureSpec.MODE_SHIFT) - 1) int size,
@MeasureSpecMode int mode) {
if (sUseBrokenMakeMeasureSpec) {
return size + mode;
} else {
return (size & ~MODE_MASK) | (mode & MODE_MASK);
}
}
该方法的返回size & ~MODE_MASK 表示传递的size和MODE_MASK取反,然后在做与操作。
例MODE_MASK = 11000000000000000000000000000000
~MODE_MASK = 00111111111111111111111111111111
这里实际是对MeasureSpec的打包生成操作,size & ~MODE_MASK取的是MODE_MASK后30位,mode & MODE_MASK取的是MODE_MASK前2位,这样就生成了一个新的MeasureSpec对象。
public static int getMode(int measureSpec) {
//noinspection ResourceType
return (measureSpec & MODE_MASK);
}
public static int getSize(int measureSpec) {
return (measureSpec & ~MODE_MASK);
}
MeasureSpec提供的这两个方法,表示我们可以将传入的MeasureSpec进行解包操作,获取相应的mode和size值。
这里的三个模式代表什么意思呢
UNSPECIFIED 表示父容器不对子view做限制
EXACTLY 表示父容器检测出子view的大小,其大小就是SpecSize,对应的LayoutParams就是match_parent或者设定的dp值
AT_MOST 表示父容器最多可以给子view的大小,其大小最多就是parent的大小,对应的LayoutParams是wrap_content
到这里,回到getRootMeasureSpec(int windowSize, int rootDimension)方法中,windowSize是窗口大小,rootDimension则是由DecorView的LayoutParams传递的,那么由此得知,DecorView的大小由窗口大小和其自身的LayoutParams共同决定。
接着回到performMeasure()方法中,发现其调用了 mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);方法,而在measure()方法中又调用了onMeasure()方法,我们知道mView的本质是DecorView对象,而DecorView实际上是FrameLayout,由此我们直接进入FrameLayout的onMeasure()方法。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
//...
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) {
//注释1
measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
//注释2
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);
}
}
}
}
//注释3
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
resolveSizeAndState(maxHeight, heightMeasureSpec,
childState << MEASURED_HEIGHT_STATE_SHIFT));
//...
if (count > 1) {
for (int i = 0; i < count; i++) {
final View child = mMatchParentChildren.get(i);
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
//...
//...
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
}
}
在注释1中,measureChildWithMargins()方法是测量子view,在该方法中调用getChildMeasureSpec()方法,解析子view的MeasureSpec。
在注释2中,获取的maxWidth 以及maxHeight ,用来确定子view所占的最大宽度和最大高度。
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);
}
上述代码中定义的specMode,是解析父容器的mode值
父容器是类型:
1)EXACTLY
如果设置了子view的宽高值(childDimension>0),那么子view的specSize就是设定的值childDimension,而specMode就是EXACTLY类型。
如果子view是MATCH_PARENT(childDimension=MATCH_PARENT),那么子view的specSize就是父容器的size,而specMode就是EXACTLY类型。
如果子view是WRAP_CONTENT(childDimension=WRAP_CONTENT),那么子view的specSize就是父容器的size,而specMode就是AT_MOST类型。
2)AT_MOST
如果设置了子view的宽高值(childDimension>0),那么子view的specSize就是设定的值childDimension,而specMode就是EXACTLY类型。
如果子view是MATCH_PARENT(childDimension=MATCH_PARENT),那么子view的specSize就是父容器的size,而specMode就是AT_MOST类型。
如果子view是WRAP_CONTENT(childDimension=WRAP_CONTENT),那么默认子view的specSize就是父容器的size,而specMode就是AT_MOST类型。
3)UNSPECIFIED
如果设置了子view的宽高值(childDimension>0),那么子view的specSize就是设定的值childDimension,而specMode就是EXACTLY类型。
如果子view是MATCH_PARENT(childDimension=MATCH_PARENT),那么子view的specSize就是0(View.sUseZeroUnspecifiedMeasureSpec为true时,specSize是父容器的size),而specMode就是UNSPECIFIED类型。
如果子view是WRAP_CONTENT(childDimension=WRAP_CONTENT),那么子view的specSize就是0(View.sUseZeroUnspecifiedMeasureSpec为true时,specSize是父容器的size),而specMode就是UNSPECIFIED类型。
View的MeasureSpec由父容器的SpecMode和自身的LayoutParams决定
回到FrameLayout的onMeasure()方法中,调用了setMeasuredDimension(int measuredWidth, int measuredHeight),在注释3中,传递了子View的最大宽度、最大高度以及padding值,其目的是为了计算出该父容器的宽高值,接着又调用了setMeasuredDimensionRaw()方法,完成了ViewGroup的测量。
下面来看View的onMeasure()方法
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
这里先看一下getDefaultSize()方法
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;
}
该方法说明不管测量的mode是AT_MOST还是EXACTLY,它的大小 都是返回的specSize,在上述表中,不管设置子view的LayoutParams是match_parent还是wrap_content,它的size都是parentSize,说明在自定义view中,如果不重写onMeasure()方法,xml中设置match_parent或者wrap_content,它俩的效果是一样的。
回到setMeasuredDimension()方法中,接着调用了setMeasuredDimensionRaw(),至此view的测量完成。
View测量过程
View measure()----->onMeasure()---->setMeasuredDimension()—>setMeasuredDimensionRaw()
ViewGroup的测量过程
ViewGroup measure()----->onMeasure()这里会测量子view的宽高measureChildWithMargins()---->setMeasuredDimension()—>setMeasuredDimensionRaw()
标签:MeasureSpec,int,childDimension,流程,AndroidView,LayoutParams,view,绘制,size 来源: https://blog.csdn.net/lp598876762/article/details/93872522