View的尺寸测量SpecMode&MeasureSpec
标签: View的尺寸测量SpecMode&MeasureSpec JavaScript博客 51CTO博客
2023-05-30 18:24:16 201浏览
View地绘制流程

自定义绘制流程

我们都是知道Androdi的视图数在创建时回掉用视图的measure、layout、draw三个函数,分别对应尺寸测量、视图布局、绘制内容。但是,对于非ViewGroup类型来说,layout这个步骤不需要的,因为它并不是一个视图容器。它需要做的工作只是测量尺寸与绘制自身内容,上述SimpleImageview就是这样的例子。
但是,SImpleImageView的尺寸测量只能根据图片的大小进行设置,如果用户像支持需要根据用户设置的宽高模式来计算SimpleImageview的尺寸,而不是一概地使用图片地宽高值作为视图地宽高。
在视图树渲染时View系统地绘制流程会从ViewRoot地performTraversals()方法中开始,在其内部调用View的measure()方法。measure()方法接收两个参数:widthMeasureSpec和MesaureSpec的值由specSize和specMode共同组成,其中specSize记录的时大小,specMode记录的时规格。在支持match_parent、具体的宽高值之前,我们需要了解specMode的3种类型,如表2-1所示。
模式类型 |
说明 |
EXACTLY |
表示父视图希望子视图的大小应该由specSize的值来决定的,系统默认会按照这个规则来设置子视图的大小,开发人员当然也可以按照自己的意愿设置成任意的大小,match_parent、具体的数值(如:100dp)对用的都是这个模式 |
AT_MOST |
表示子视图最多只能是specSize中指定的大小,开发人员你应该尽可能小地去设置这个 视图,并且保证不会超过specSize。系统默认会按照这个规则来设置子视图地带线啊哦,开发人员当然也可以按照自己地意愿设置成任意地大小、一般来说wrap_content对应这种模式 |
UNSPECIFIED |
表示开发人员可以将视图按照自己地以验设置成任意地大小,没有任何限制。这种情况比较少见,不太会用到。 |
MeasureSpec时View种地内部类,基本都是二进制计算。由于int时32位地,用高两位表示mode,低30位表示size,MODE_SHIFT=30地作用时移位
UNSPECIFIED:不对view大小做限制,系统使用
EXACTLY:确切地大小,如:100dp
AT_MOST:大小不可超过某数值,如:match_parent,最大不能超过你爸爸



那么这两个MeasureSpec又是从哪里来的呢?其实这是从整个视图树控制类ViewRootImpl创建地,在ViewRootImpl地measuireHierarchy函数中会调用如下代码获取MeasureSpec:
if (!goodMeasure) {
childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
if (mWidth != host.getMeasuredWidth() || mHeight != host.getMeasuredHeight()) {
windowSizeMayChange = true;
}
}
从伤处程序中看到,这里调用了getRootMesaureSpec()方法来获取widthMesasureSpec和heightMeasureSpec的值。注意,方法种传入的参数,参数为窗口的宽度或者高度,而lp.width和lp.height在创建ViewGroup实例时就被赋值了,它们都等于MATCH_PARENT。然后再看一下gerRootMeasureSpec()方法的代码,如下显示:
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.makeMeasureSpec()方法装一个MeasureSpec,当rootDimension参数等于MATCH_PARENT时,MeasureSpec的specMode就等于EXACTLY,当rootDimension等于WRAP_CONTENT时,MeasureSpec的specMode就等于AT_MOST;并且MATCH_PARENT和WRAP_CONTENT的specSize等于windowSize的,也就以为只根视图总会充满全屏的。
当构建完根视图的MeasureSpec之后就会执行performMeasure函数从根视图开始一层一侧测量视图的大小。最终会调用每个View的onMeasure函数,再该函数种用户根据MeasureSpec测量View的大小,最终调用setMeasuredDimension函数设置该视图的大小。下面我们看看SimpleImageview根据MeasureSpec设置大小的实现,修改变得部分只有测量视图的部分,代码如下:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//获取宽度的模式与大小
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
//高度的模式与大小
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
//设置View的宽高
setMeasuredDimension(measureWidth(widthMode, width), measureHeight(heightMode, height));
}
private int measureWidth(int mode, int width) {
switch (mode) {
case MeasureSpec.UNSPECIFIED:
case MeasureSpec.AT_MOST:
break;
case MeasureSpec.EXACTLY:
mWidth = width;
break;
}
return mWidth;
}
private int measureHeight(int mode, int height) {
switch (mode) {
case MeasureSpec.UNSPECIFIED:
case MeasureSpec.AT_MOST:
break;
case MeasureSpec.EXACTLY:
mHeight = height;
break;
}
return mHeight;
}
@Override
protected void onDraw(Canvas canvas) {
//super.onDraw(canvas);
if (mBitmap == null) {
mBitmap = Bitmap.createScaledBitmap(bitmap, getMeasuredWidth(), getMeasuredHeight(), true);
}
//绘制图片
canvas.drawBitmap(mBitmap, getLeft(), getTop(), mBitmapPaint);
}
在onMeasure函数种我们获取宽高的模式与大小,然后分别调用measureWidth、measureHeigh函数根据MeasureSpec的mode与大小计算View的具体大小。在MeasureSpec.UNSPECIFIED与MeasureSpec,AT_MOST类型种,我们都将View的宽高设置为图片的宽高,而用户制定了具体的大小或match_parent时,它的模式则为EXACTLY,它的值就是MeasureSpec中的值。最后在绘制图片时,会根据View的大小重新创建于给图片,得到一个与View大小一致的Bitmap,然后会知道View上。
图分别为宽高设置为wrap_content、match_parnt、具体值的显示效果。

View的测量时自定义View中最为重要的一步,如果不能正确地测量视图地大小,那么将会导致视图显示不完整等情况,这将严重影响View地显示效果。因此,理解MeasureSpec以及正确地测量方法对于开发人员来说时必不可少的。
MesureSpec
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;
/**
* Creates a measure specification based on the supplied size and mode.
*
* The mode must always be one of the following:
* <ul>
* <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
* <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
* <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
* </ul>
*
* <p><strong>Note:</strong> On API level 17 and lower, makeMeasureSpec's
* implementation was such that the order of arguments did not matter
* and overflow in either value could impact the resulting MeasureSpec.
* {@link android.widget.RelativeLayout} was affected by this bug.
* Apps targeting API levels greater than 17 will get the fixed, more strict
* behavior.</p>
*
* @param size the size of the measure specification
* @param mode the mode of the measure specification
* @return the measure specification based on size and mode
*/
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);
}
}
/**
* Like {@link #makeMeasureSpec(int, int)}, but any spec with a mode of UNSPECIFIED
* will automatically get a size of 0. Older apps expect this.
*
* @hide internal use only for compatibility with system widgets and older apps
*/
@UnsupportedAppUsage
public static int makeSafeMeasureSpec(int size, int mode) {
if (sUseZeroUnspecifiedMeasureSpec && mode == UNSPECIFIED) {
return 0;
}
return makeMeasureSpec(size, mode);
}
/**
* Extracts the mode from the supplied measure specification.
*
* @param measureSpec the measure specification to extract the mode from
* @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
* {@link android.view.View.MeasureSpec#AT_MOST} or
* {@link android.view.View.MeasureSpec#EXACTLY}
*/
@MeasureSpecMode
public static int getMode(int measureSpec) {
//noinspection ResourceType
return (measureSpec & MODE_MASK);
}
/**
* Extracts the size from the supplied measure specification.
*
* @param measureSpec the measure specification to extract the size from
* @return the size in pixels defined in the supplied measure specification
*/
public static int getSize(int measureSpec) {
return (measureSpec & ~MODE_MASK);
}
static int adjust(int measureSpec, int delta) {
final int mode = getMode(measureSpec);
int size = getSize(measureSpec);
if (mode == UNSPECIFIED) {
// No need to adjust size for UNSPECIFIED mode.
return makeMeasureSpec(size, UNSPECIFIED);
}
size += delta;
if (size < 0) {
Log.e(VIEW_LOG_TAG, "MeasureSpec.adjust: new size would be negative! (" + size +
") spec: " + toString(measureSpec) + " delta: " + delta);
size = 0;
}
return makeMeasureSpec(size, mode);
}
/**
* Returns a String representation of the specified measure
* specification.
*
* @param measureSpec the measure specification to convert to a String
* @return a String with the following format: "MeasureSpec: MODE SIZE"
*/
public static String toString(int measureSpec) {
int mode = getMode(measureSpec);
int size = getSize(measureSpec);
StringBuilder sb = new StringBuilder("MeasureSpec: ");
if (mode == UNSPECIFIED)
sb.append("UNSPECIFIED ");
else if (mode == EXACTLY)
sb.append("EXACTLY ");
else if (mode == AT_MOST)
sb.append("AT_MOST ");
else
sb.append(mode).append(" ");
sb.append(size);
return sb.toString();
}
好博客就要一起分享哦!分享海报
此处可发布评论
评论(0)展开评论
展开评论





