通上面的文章我们了解到了View加载与绘制的方法调用的流程。本文中主要是对View中的onMeasure()、onLayout()、onDraw()进行讲解。
(一)onMeasure方法
该方法是用来测量控件的大小的。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
widthMeasureSpec、heightMeasureSpec这两个参数是怎么来的呢?
int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
// Ask host how big it wants to be
performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
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;
}
public static class MeasureSpec {
private static final int MODE_SHIFT = 30;
// 二进制 11000000000000000000000000000000
private static final int MODE_MASK = 0x3 << MODE_SHIFT;
// 二进制 00000000000000000000000000000000
public static final int UNSPECIFIED = 0 << MODE_SHIFT;
// 二进制 01000000000000000000000000000000
public static final int EXACTLY = 1 << MODE_SHIFT;
// 二进制 10000000000000000000000000000000
public static final int AT_MOST = 2 << MODE_SHIFT;
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);
}
}
public static int getMode(int measureSpec) {
//noinspection ResourceType
return (measureSpec & MODE_MASK);
}
public static int getSize(int measureSpec) {
return (measureSpec & ~MODE_MASK);
}
}
首先我们要了解一下MeasureSpec这个类。
private static final int MODE_SHIFT = 30;
private static final int MODE_MASK = 0x3 << MODE_SHIFT;
getSize中的返回值 (measureSpec & ~MODE_MASK)
measureSpec 是我们传入的一个int值 以 13 为例 二进制为00000000 00000000 00000000 00001111
MODE_MASK 的二进制是 11000000 00000000 00000000 00000000
~MODE_MASK 取反后 00111111 11111111 11111111 11111111
(measureSpec & ~MODE_MASK) 的值为
00000000 00000000 00000000 00001111
& 00111111 11111111 11111111 11111111
—————————————————————————————————————
00000000 00000000 00000000 00001111
&运算除了 真&真为真 其他都为假 通过运算我们可以看出来getSize返回的就是传入值的二进制的后30位
==============================================================================================
getMode中的返回值 (measureSpec & MODE_MASK)
measureSpec 还以13为例,二进制为00000000 00000000 00000000 00001111
MODE_MASK 的二进制是 11000000 00000000 00000000 00000000
(measureSpec & MODE_MASK) 的值为
00000000 00000000 00000000 00001111
& 11000000 00000000 00000000 00000000
——————————————————————————————————————
00000000 00000000 00000000 00000000
通过运算取的是32位的前两位的值后30位为0
通过上面的运算分析我们可以得出测量值二进制的前两位表示测量模式,后30位表示实际测量值。
MeasureSpecMode有三种模式
- UNSPECIFIED 系统使用的模式
- EXACTLY 精准模式 此时的size就是View的实际大小
- AT_MOST 最大模式
通过getRootMeasureSpec()方法可以看出,measureSpecMode的值是根据View设置的宽高属性决定的。在设置MATCH_CONTENT和实际值的情况下测量模式就是EXACTLY,设置WARP_CONTENT的时候测量模式就是AT_MOST。
我们了解了measureSpec之后,在看一下setMeasuredDimension()方法
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
boolean optical = isLayoutModeOptical(this);
if (optical != isLayoutModeOptical(mParent)) {
Insets insets = getOpticalInsets();
int opticalWidth = insets.left + insets.right;
int opticalHeight = insets.top + insets.bottom;
measuredWidth += optical ? opticalWidth : -opticalWidth;
measuredHeight += optical ? opticalHeight : -opticalHeight;
}
setMeasuredDimensionRaw(measuredWidth, measuredHeight);
}
private void setMeasuredDimensionRaw(int measuredWidth, int measuredHeight) {
mMeasuredWidth = measuredWidth;
mMeasuredHeight = measuredHeight;
mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
}
setMeasuredDimension()方法就是设置控件的实际宽高。如果在onMeasure()方法中不调用该方法的话就会报错。
onMeasure()方法在View中只测量自己就可以了,在ViewGroup中除了要测量自己还要遍历所有的子View进行测量child.measure()方法。调用child.measure()方法前要先获取子View的measureSpec。通过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);
}
通过上面的方法可以看出子View的measureSpec和父控件的measureSpec有关。
上图就是子View的measureSpec的获取关系图
(二)onLayout方法
控件位置的摆放。
子View会通过layout()方法进行摆放不需要重写onLayout()方法,自定一的ViewGroup要通过重写onLayout()方法来摆放子View的位置。
(三)onDraw方法
View的绘制步骤
/*
* Draw traversal performs several drawing steps which must be executed
* in the appropriate order:
*
* 1. Draw the background
* 2. If necessary, save the canvas' layers to prepare for fading
* 3. Draw view's content
* 4. Draw children
* 5. If necessary, draw the fading edges and restore layers
* 6. Draw decorations (scrollbars for instance)
*/
- 绘制背景
- 如果有必要就保存图层
- 绘制视图内容 onDraw(canvas)
- 绘制子View dispatchDraw(canvas)
- 如果有必要,绘制褪色的边缘并保存图层
- 绘制装饰(如滚动条)
(四)requestLayout()、invalidate()和postInvalidate()
requestLayout() 重新进行测量、摆放、绘制。主线程调用。
invalidate() 重新进行绘制。主线程调用。
postInvalidate() 重新进行绘制。可以在子线程调用,方法内部使用了handler切换线程。
该文档是自己的学习记录如有错误欢迎指出。