Android MeasureSpec详解

简介

我们在自定义控件的时候经常要重写View的onMeasure方法,onMeasure方法有两个int类型的参数,这两个参数就是MeasureSpec,这两个参数可不是普通的int数值,里面包含了mode和size两个信息,一个int有32位二进制,用高2位表示mode,低30位表示size,这样在一个int里面包含两个数值信息的设计,是为了减少对象的创建和内存的分配

onMeasure()方法用来测量空间的宽高,方法中的MeasureSpec参数由控件的父布局传入,是父View对子View的宽高的约束。

要注意onMeasure()方法的参数类型是int而不是MeasureSpec,MeasureSpec只是一个工具类,用来打包和拆包mode和size

源码

在讲解前,我建议先看下MeasureSpec的源码和源码的注释,源码很简单,注释里也讲的很清楚

为了更便于阅读,以下源码是比较早的安卓版本Android 4.1(Jelly Bean),API16

    /**
     * A MeasureSpec encapsulates the layout requirements passed from parent to child.
     * Each MeasureSpec represents a requirement for either the width or the height.
     * A MeasureSpec is comprised of a size and a mode. There are three possible
     * modes:
     * <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 static class MeasureSpec {
        private static final int MODE_SHIFT = 30;
        private static final int MODE_MASK  = 0x3 << MODE_SHIFT;

        /**
         * 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>
         *
         * @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) {
            return 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}
         */
        public static int getMode(int measureSpec) {
            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);
        }

        /**
         * 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();
        }
    }

mode

上面说过MeasureSpec的高两位表示mode,mode有三种类型

UNSPECIFIED:父View对子View没有任何限制,子View的宽高想多大就多大

EXACTLY:父View已经替子View确定了大小,不管子View要多大都不行

AT_MOST:子View要多大就多大,但是不能超过上限

掩码Mask和mode常量

在上面的源码中可以看到三种mode的定义及mask的定义,可以再看一下

        private static final int MODE_SHIFT = 30;
        private static final int MODE_MASK  = 0x3 << MODE_SHIFT;

        /**
         * 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;

MODE_SHIFT=30,这个常量下面会用到,做一些位移的操作

MODE_MASK就是掩码,0x3就是二进制0b11,向左位移30位后就是0b11000000000000000000000000000000,这个就可以作为mode的掩码,和int类型的MeasureSpec做位与运算就可以得到mode的值,后面会做详细介绍

UNSPECIFIED模式的值为0,0 << MODE_SHIFT位移运算后其实值还是零,0b00000000000000000000000000000000,高二位为00

EXACTLY模式的值为1 << MODE_SHIFT,也就是1左移30位,用二进制表示为0b01000000000000000000000000000000,高二位为01

int AT_MOST模式的值为2 << MODE_SHIFT,2用二进制表示为0b10,也就是0b10左移30位,用二进制表示为0b10000000000000000000000000000000,高二位为10

组装mode和size

MeasureSpec是一个int类型的数值,由mode和size组成,高二位表示mode,低30位表示size

mode必须为UNSPECIFIED,AT_MOST,EXACTLY三种类型的一种

方法很简单,就是把参数size和mode相加就完事了

比如:

0b01000000000000000000000000000000,mode为EXACTLY

0b00000000000000000000000000000011,size为3

0b01000000000000000000000000000011,这就是相加后的结果measure specification

代码如下所示,非常简单

        /**
         * 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>
         *
         * @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) {
            return size + mode;
        }

解析mode

解析MeasureSpec中的mode也是很简单,保留高2位,其余低30位置0就可以了

掩码MODE_MASK值为0b11000000000000000000000000000000,将MeasureSpec和掩码MODE_MASK做位与运算,就可以保留高2位,其余低30位置零,得到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}
         */
        public static int getMode(int measureSpec) {
            return (measureSpec & MODE_MASK);
        }

解析size

解析size和解析mode的方法类似,只是把高2位置零,其余低30位保留

先把掩码MODE_MASK做按位取反,得到0b00111111111111111111111111111111,这个就是size的掩码,再把该掩码和MeasureSpec做位与运算,就得到size的值

size的单位是px

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

格式化打印输出

这个方法就是把MeasureSpec格式化下打印输出,可以看该MeasureSpec包含的mode和size信息

格式为"MeasureSpec: MODE SIZE",比如"MeasureSpec: EXACTLY 100"

        /**
         * 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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值