Android View measure解读

measure源码

    public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
        //判断是否使用视觉边界布局
        boolean optical = isLayoutModeOptical(this);
        if (optical != isLayoutModeOptical(mParent)) {
            Insets insets = getOpticalInsets();
            int oWidth  = insets.left + insets.right;
            int oHeight = insets.top  + insets.bottom;
            widthMeasureSpec  = MeasureSpec.adjust(widthMeasureSpec,  optical ? -oWidth  : oWidth);
            heightMeasureSpec = MeasureSpec.adjust(heightMeasureSpec, optical ? -oHeight : oHeight);
        }

        // Suppress sign extension for the low bytes
        //计算key值
        long key = (long) widthMeasureSpec << 32 | (long) heightMeasureSpec & 0xffffffffL;
      
         //mMeasureCache是LongSparseLongArray类型的成员变量,
         //其缓存着View在不同widthMeasureSpec、heightMeasureSpec下量算过的结果
         //如果mMeasureCache为空,我们就新new一个对象赋值给mMeasureCache
        if (mMeasureCache == null) 
          mMeasureCache = new LongSparseLongArray(2);
      
        //是否强制测量
        final boolean forceLayout = (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;

        // Optimize layout by avoiding an extra EXACTLY pass when the view is
        // already measured as the correct size. In API 23 and below, this
        // extra pass is required to make LinearLayout re-distribute weight.
        //mOldWidthMeasureSpec和mOldHeightMeasureSpec分别表示上次对View进行量算时的widthMeasureSpec和heightMeasureSpec
        //执行View的measure方法时,View总是先检查一下是不是真的有必要费很大力气去做真正的量算工作
        final boolean specChanged = widthMeasureSpec != mOldWidthMeasureSpec
                || heightMeasureSpec != mOldHeightMeasureSpec;
        //parent是否给定了精确的大小
        final boolean isSpecExactly = MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY
                && MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY;
        final boolean matchesSpecSize = getMeasuredWidth() == MeasureSpec.getSize(widthMeasureSpec)
                && getMeasuredHeight() == MeasureSpec.getSize(heightMeasureSpec);
      
        final boolean needsLayout = specChanged
                && (sAlwaysRemeasureExactly || !isSpecExactly || !matchesSpecSize);

        if (forceLayout || needsLayout) {
            // first clears the measured dimension flag
            //通过按位操作,重置View的状态mPrivateFlags,将其标记为未量算状态
            mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
            //对阿拉伯语、希伯来语等从右到左书写、布局的语言进行特殊处理
            resolveRtlPropertiesIfNeeded();

            //在View真正进行量算之前,View还想进一步确认能不能从已有的缓存mMeasureCache中读取缓存过的量算结果
            //如果是强制layout导致的量算,那么将cacheIndex设置为-1,即不从缓存中读取量算结果
            //如果不是强制layout导致的量算,那么我们就用上面根据measureSpec计算出来的key值作为缓存索引cacheIndex。
            int cacheIndex = forceLayout ? -1 : mMeasureCache.indexOfKey(key);
            if (cacheIndex < 0 || sIgnoreMeasureCache) {
                // measure ourselves, this should set the measured dimension flag back
                //如果运行到此处,表示我们没有从缓存中找到量算过的尺寸或者是sIgnoreMeasureCache为true导致我们要忽略缓存结果
                //此处调用onMeasure方法,并把尺寸限制条件widthMeasureSpec和heightMeasureSpec传入进去
                //onMeasure方法中将会进行实际的量算工作,并把量算的结果保存到成员变量中
                onMeasure(widthMeasureSpec, heightMeasureSpec);
                //onMeasure执行完后,通过位操作,重置View的状态mPrivateFlags,将其标记为在layout之前不必再进行量算的状态
                mPrivateFlags3 &= ~PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT;
            } else {
                //onMeasure执行完后,通过位操作,重置View的状态mPrivateFlags,将其标记为在layout之前不必再进行量算的状态
                long value = mMeasureCache.valueAt(cacheIndex);
                // Casting a long to int drops the high 32 bits, no mask needed
                //一旦我们从缓存中读到值,我们就可以调用setMeasuredDimensionRaw方法将当前量算的结果到成员变量中
                setMeasuredDimensionRaw((int) (value >> 32), (int) value);
                mPrivateFlags3 |= PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT;
            }

            // flag not set, setMeasuredDimension() was not invoked, we raise
            // an exception to warn the developer
            //如果我们自定义的View重写了onMeasure方法,但是没有调用setMeasuredDimension()方法,
            //那么此处就会抛出异常,提醒开发者在onMeasure方法中调用setMeasuredDimension()方法
            //Android是如何知道我们有没有在onMeasure方法中调用setMeasuredDimension()方法的呢?
            //方法很简单,还是通过解析状态位mPrivateFlags。
            //setMeasuredDimension()方法中会将mPrivateFlags设置为PFLAG_MEASURED_DIMENSION_SET状态,即已量算状态,
            //此处就检查mPrivateFlags是否含有PFLAG_MEASURED_DIMENSION_SET状态即可判断setMeasuredDimension是否被调用
            if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
                throw new IllegalStateException("View with id " + getId() + ": "
                        + getClass().getName() + "#onMeasure() did not set the"
                        + " measured dimension by calling"
                        + " setMeasuredDimension()");
            }

            mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
        }

        //mOldWidthMeasureSpec和mOldHeightMeasureSpec保存着最近一次量算时的MeasureSpec,
        //在量算完成后将这次新传入的MeasureSpec赋值给它们
        mOldWidthMeasureSpec = widthMeasureSpec;
        mOldHeightMeasureSpec = heightMeasureSpec;

        //最后用上面计算出的key作为键,量算结果作为值,将该键值对放入成员变量mMeasureCache中,
        //这样就实现了对本次量算结果的缓存,以便在下次measure方法执行的时候,有可能将其从中直接读出,
        //从而省去实际量算的步骤
        mMeasureCache.put(key, ((long) mMeasuredWidth) << 32 |
                (long) mMeasuredHeight & 0xffffffffL); // suppress sign extension
    }
  • 判断是否使用视觉边界布局,默认为否,如果使用,需要重新布局
  • 根据widthMeasureSpec、heightMeasureSpec计算缓存key
  • 判断是否需要重新计算尺寸
    • specChanged,将当前widthMeasureSpec、heightMeasureSpec和旧的值比较。
    • isSpecExactly,是否使用了精确大小布局
    • forceLayout,mPrivateFlags & PFLAG_FORCE_LAYOUT。是否有强制布局标识
  • 如果需要重新计算
    • 去除PFLAG_MEASURED_DIMENSION_SET标识
    • 获取缓存index
    • 如果缓存index=-1或者已经标识忽略缓存,则重新计算
      • onMeasure(widthMeasureSpec, heightMeasureSpec);
      • 置View的状态mPrivateFlags,将其标记为在layout之前不必再进行量算的状态
    • 如果有缓存并未标识忽略缓存则直接使用缓存尺寸setMeasuredDimensionRaw()
  • 判断PFLAG_MEASURED_DIMENSION_SET是否设置
    • PFLAG_MEASURED_DIMENSION_SET在setMeasuredDimensionRaw()方法中设置,所以onMeasure()方法中必须调用setMeasuredDimensionRaw方法
  • 设置mOldWidthMeasureSpec、mOldHeightMeasureSpec并缓存尺寸

流程图:

st=>start: measure
cond=>condition: 是否使用视觉布局?
op_calc=>operation: 重新计算尺寸
op_gen_cache_key=>operation: 根据尺寸计算缓存key
cond_isLayout=>condition: 是否需要强制布局(与old尺寸比较||强制布局标识)
op_et_cache=>operation: 清除尺寸计算标示,获取缓存尺寸
cond_is_cache=>condition: 是否存在缓存||忽略缓存
op_on_measure=>operation: 计算并设置尺寸
op_use_cache=>operation: 使用缓存尺寸
cond_is_set_measure=>condition: 是否设置尺寸(setMeasuredDimensionRaw)
op_no_set_measuret=>operation: IllegalStateException
op_reset=>operation: 设置就尺寸,缓存尺寸
e=>end: 计算尺寸结束

st->cond->op
cond(no)->op_gen_cache_key
cond(yes)->op_calc
op_calc->op_gen_cache_key
op_gen_cache_key->cond_isLayout
cond_isLayout(yes)->op_et_cache
cond_isLayout(no)->op_reset
op_et_cache->cond_is_cache
cond_is_cache(no)->op_on_measure
cond_is_cache(yes)->op_use_cache
op_use_cache->cond_is_set_measure
op_on_measure->cond_is_set_measure
cond_is_set_measure(no)->op_no_set_measuret
cond_is_set_measure(yes)->op_reset
op_reset->e

你可能感兴趣的:(Android View measure解读)