Android中View内部类MeasureSpec研究

终于建了一个自己个人小站:https://huangtianyu.gitee.io,以后优先更新小站博客,欢迎进站,O(∩_∩)O~~

当我们使用自定义View或ViewGroup的时候,经常会遇到MeasureSpec类,这个类主要用于在onMeasure中表示该控件的大小。在该变量里面包含该控件尺寸的大小和模式。这个模式主要表明该控件能得到的尺寸类型,包括三个模式:UNSPECIFIED未指定模式,EXACTLY精确模式,AT_MOST最大模式。

1.EXACTLY

在这种模式下,给定尺寸的值是多少,那么这个控件的长或宽就是多少。

2.AT_MOST

当前控件的长或宽最大只能是父控件能够给的最大空间,当然也可以比这个小。

3.UNSPECIFIED

当前控件可以随便用空间,不受限制。

模式有三种,至少得2位二进制位。于是系统采用了最高的2位表示模式。在Android中通过int的前两位来表示当前int值的模式类型。如图:


其中00的表示"未指定模式"。即MeasureSpec.UNSPECIFIED

    01的表示"'精确模式"。即MeasureSpec.EXACTLY

    11的表示"最大模式"。即MeasureSpec.AT_MOST


在该类中,常用的方法有三个:

     /**
         * @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);
            }
        }
该方法由我们给出的尺寸大小和模式生成一个包含两个信息的int变量,也就是我们需要设置的长或者宽。


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

该方法是得到给定变量里面的尺寸大小的值。

其中上述几个常量的值如下:

    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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值