MeasureSpec测量规格
/**
* A MeasureSpec encapsulates the layout requirements passed from parent to child.
* MeasureSpec是父控件传给子控件的布局条件
* Each MeasureSpec represents a requirement for either the width or the height.
* 每个MeasureSpec标识着宽度或高度的限制
* A MeasureSpec is comprised of a size and a mode. There are three possible
* modes:
* MeasureSpec由size和mode组成
* <dl>
* <dt>UNSPECIFIED</dt>未指定
* <dd>
* The parent has not imposed any constraint on the child. It can be whatever size
* it wants.
* </dd>
*
* <dt>EXACTLY</dt>精确地
* <dd>
* 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.
* </dd>
*
* <dt>AT_MOST</dt>至多
* <dd>
* The child can be as large as it wants up to the specified size.
* </dd>
* </dl>
*
* MeasureSpecs are implemented as ints to reduce object allocation.
*
* This class is provided to pack and unpack the <size, mode> tuple into the int.
*/
public class MeasureSpec {
......
}
标记位
一个整型是32比特位,MeasureSpec用前2位表示SpecMode,后30位表示SpecSize,它们的操作涉及到的知识点:移位运算、与或非操作参考我另外两篇博客,
java二进制、八进制、十六进制间转换详细
java位运算示例
private static final int MODE_SHIFT = 30;
private static final int MODE_MASK = 0x3 << MODE_SHIFT;//11000000 00000000 00000000 00000000
public static final int UNSPECIFIED = 0 << MODE_SHIFT;//00000000 00000000 00000000 00000000
public static final int EXACTLY = 1 << MODE_SHIFT;//01000000 00000000 00000000 00000000
public static final int AT_MOST = 2 << MODE_SHIFT;//10000000 00000000 00000000 00000000
SpaceMode
- UNSPECIFIED
父容器不对View有任何限制,要多大给多大,这种情况一般用于系统内部,表示测量的状态 - EXACTLY
父容器已经检测出View所需要的精确大小,这个时候View的最终大小就是SpecSize指定的值。他对应于LayoutParams中的match_parent和具体的数值这两种模式 - AT_MOST
父容器制定了一个可用大小即SpecSize,View的大小不能大于这个值,具体是什么值要看不同View的具体实现。它对应于LayoutParams中的wrap_content
相关代码
1.MeasureSpec被实例化之后,通过adjust将SpacMode、SpecSize打包成int值
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);
}
方法:makeMeasureSpec
/**
* Creates a measure specification based on the supplied size and mode.
* 创建一个基于size和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>
*
* @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(int size, int mode) {
if (sUseBrokenMakeMeasureSpec) {
return size + mode;
} else {
return (size & ~MODE_MASK) | (mode & MODE_MASK);
}
}
也可以外部调用,通过makeSafeMeasureSpec方法打包int值
/**
* Like {@link #makeMeasureSpec(int, int)}, but any spec with a mode of UNSPECIFIED
* will automatically get a size of 0. Older apps expect(期望) this.
* 当spec的mode为UNSPECIFIED时,size的值自动设置为0
* @hide internal use only for compatibility(兼容) with system widgets and older apps
*/
public static int makeSafeMeasureSpec(int size, int mode) {
if (sUseZeroUnspecifiedMeasureSpec && mode == UNSPECIFIED) {
return 0;
}
return makeMeasureSpec(size, mode);
}
2.可以通过MeasureSpec的getMode、getSize获取相关信息
方法:getMode
/**
* Extracts the mode from the supplied measure specification.
* 从measureSpec中抽取mode
* @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}
*/
public static int getMode(int measureSpec) {
return (measureSpec & MODE_MASK);
}
方法:getSize
/**
* 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);
}
作者:u010137760 发表于2016/9/27 9:38:54 原文链接
阅读:1 评论:0 查看评论