MeasureSpec解析

MeasureSpec解析MeasureSpec测量规格/***AMeasureSpecencapsulatesthelayoutrequirementspassedfromparenttochild.*MeasureSpec是父控件传给子控件的布局条件*EachMeasureSpecrepresentsarequirementforeitherthewidthort

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);
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/36529.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注