View静态类MeasureSpec

作者: 721d739b6619 | 来源:发表于2017-10-12 20:18 被阅读32次

    源码的注解:

    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:UNSPECIFIED;EXACTLY;AT_MOST
    MeasureSpecs are implemented as ints to reduce object allocation. This class is provided to pack and unpack the <size, mode> tuple into the int.

    大意:该MeasureSpec封装了布局所需的有父传给子;每个MeasureSpec都代表宽度和高度的要求;一个MeasureSpec包含size和mode。mode有三种:UNSPECIFIED;EXACTLY;AT_MOST;MeasureSpec的实现为了降低对象的开销。该类提供了分解和组合size和mode的方法。

    源码:

    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>
             *
             * <p><strong>Note:</strong> On API level 17 and lower, makeMeasureSpec's
             * implementation was such that the order of arguments did not matter
             * and overflow in either value could impact the resulting MeasureSpec.
             * {@link android.widget.RelativeLayout} was affected by this bug.
             * Apps targeting API levels greater than 17 will get the fixed, more strict
             * behavior.</p>
             *
             * @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);
                }
            }
    
            /**
             * 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);
            }
    
            static int adjust(int measureSpec, int delta) {
                final int mode = getMode(measureSpec);
                if (mode == UNSPECIFIED) {
                    // No need to adjust size for UNSPECIFIED mode.
                    return makeMeasureSpec(0, UNSPECIFIED);
                }
                int size = getSize(measureSpec) + 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);
            }
    
            /**
             * 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();
            }
        }
    

    其实没有多少内容;看看关键的几个变量和方法:

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

    MODE_SHIFT 就是10进制的30;
    MODE_MASK 是16进制的 0x3 加上 向左移 30个位
    0x3是即十进制的 3 二进制 11 左移30位,即
    11 后面紧跟30个0:11 000000 00000000 00000000 00000000
    MODE_MASK:11 000000 00000000 00000000 00000000

    UNSPECIFIED 根据上面的计算即是:
    00 000000 00000000 00000000 00000000

    public static final int EXACTLY = 1 << MODE_SHIFT;
    public static final int AT_MOST = 2 << MODE_SHIFT;

    EXACTLY 即是1左移30位:
    01 000000 00000000 00000000 00000000
    AT_MOST 即是2左移30位:
    10 000000 00000000 00000000 00000000

    makeMeasureSpec方法

    public static int makeMeasureSpec(int size, int mode) {
                if (sUseBrokenMakeMeasureSpec) {
                    return size + mode;
                } else {
                    return (size & ~MODE_MASK) | (mode & MODE_MASK);
                }
            }
    

    sUseBrokenMakeMeasureSpec就是一个boolean值,其实不管是true或者false,返回的结果是一样的。我们看看:
    size + mode : MeasureSpec 是一个32位的二进制值,前面两位存mode值,后面30位存size值;所以
    举例子:mode为AT_MOST
    10 000000 00000000 00000000 00000000
    size 为 00 000000 00000000 00000000 00000011
    两个数相加得出:
    10 000000 00000000 00000000 00000011

    下面看 (size & ~MODE_MASK) | (mode & MODE_MASK)
    也是用上面的值计算:
    size & ~MODE_MASK :
    size:00 000000 00000000 00000000 00000011
    ~MODE_MASK(取反) : 00 111111 11111111 11111111 11111111
    & 与运算:00 000000 00000000 00000000 00000011
    size & ~MODE_MASK :00 000000 00000000 00000000 00000011
    mode & MODE_MASK :10 000000 00000000 00000000 00000000
    最后 | 或运算:10 000000 00000000 00000000 00000011

    结果是相同的,所以旧版本是else判断那个,而新版本google开发人员将简单问题简单化了。

    public static int getMode(int measureSpec) {
    return (measureSpec & MODE_MASK);
    }

    意思就是取mode值,即measureSpec的31和32位;
    因为MODE_MASK的31和32位是1,1与运算会取回本身值。而后面30位都为0.这样返回结果就只有mode了,而除去size值。

    public static int getSize(int measureSpec) {
    return (measureSpec & ~MODE_MASK);
    }

    getSize()其实与getMode()原理一样。由于是取反“~”所以MODE_MASK取反为 00 111111 11111111 11111111 11111111
    与运算就可以除掉31位和32位的值。

    总结

    MeasureSpec这个View的内部类其实就是做一件事:
    保存size和mode值到一个32位的int值上。google开发人员十分巧妙地将两个值存放在一个int值上。这点我们在日常开发中也可以借鉴。

    相关文章

      网友评论

        本文标题:View静态类MeasureSpec

        本文链接:https://www.haomeiwen.com/subject/wssoyxtx.html