MeasureSpec解析

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 &lt;size, mode&gt; 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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果 `MeasureSpec.getSize()` 方法返回的结果为 0,可能是由于以下几种情况导致的: 1. 测量模式不正确:`MeasureSpec` 是用于测量视图尺寸的类,它包含了尺寸的测量模式和测量值。在调用 `MeasureSpec.getSize()` 方法之前,需要确保正确设置了测量模式。常见的测量模式有三种:`UNSPECIFIED`、`EXACTLY` 和 `AT_MOST`。如果测量模式为 `UNSPECIFIED`,则获取到的尺寸可能为 0。确保在测量过程中设置适当的测量模式。 2. 测量时机不正确:在获取尺寸之前,需要确保视图已经经过测量过程。在自定义 View 的 `onMeasure()` 方法中进行测量,并使用 `setMeasuredDimension()` 方法设置测量结果。如果在测量之前或者还没有完成测量过程就调用了 `MeasureSpec.getSize()` 方法,可能会得到 0 的结果。确保在合适的时机获取尺寸。 3. 视图尺寸未确定:如果视图的尺寸是根据内容动态确定的,并且在获取尺寸的时刻还没有完成布局过程,那么获取到的尺寸可能为 0。可以考虑在布局完成后再获取尺寸,例如使用 `ViewTreeObserver` 的监听器来监听布局完成事件,并在事件回调中获取尺寸。 4. 布局属性不正确:如果自定义 View 的布局属性设置不正确,可能会导致测量时无法获得准确的尺寸。确保自定义 View 在布局文件中正确设置了宽度和高度属性,并且在父布局中使用适当的布局参数。 如果以上方法仍然无法解决问题,可以进一步检查自定义 View 的代码,确保没有其他因素导致获取尺寸为 0。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值