View的工作流程--measure过程

View的measure过程

View的measure过程由其measure方法来完成(final类型)

View # measure()

public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
    // ......
    // 内部会调用onMeasure()
    onMeasure(widthMeasureSpec, heightMeasureSpec);
    // ......
}

View # onMeasure()

    // 根据期望默认最小值和MeasureSpec来确定测量宽高并保存到mMeasuredWidth/Height
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
    }

View # getSuggestedMinimumWidth/Height()

返回值就是View在UNSPECIFIED情况下的测量宽高

    protected int getSuggestedMinimumWidth() {
        // 判断View是否设置背景
        // 没有设置,View的宽度就是mMinWidth,mMinWidth对应于android:minWidth这个属性的值,若属性不指定,mMinWidth默认为0
        // 有设置背景,View的宽度就是mMinWidth和背景原始宽度的最大值
        return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
    }
    // Drawable的方法
    public int getMinimumWidth() {
        final int intrinsicWidth = getIntrinsicWidth();
        // 返回的就是Drawable的原始宽度(前提是这个Drawable有原始宽度,否则就返回0)
        return intrinsicWidth > 0 ? intrinsicWidth : 0;
    }
    // 可以重写
    // BitmapDrawable重写了(有原始宽度),ShapeDrawable没有(无原始宽度)
    public int getIntrinsicWidth() {
        return -1;
    }

View # getDefaultSize()

    public static int getDefaultSize(int size, int measureSpec) {
        int result = size;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        switch (specMode) {
        // 一般用于系统内部的测量过程,View的大小为第一个参数size即getSuggestedMinimumWidth/Height()的返回值
        case MeasureSpec.UNSPECIFIED:
            result = size;
            break;
            
        // 以下两种情况此方法返回的大小就是measureSpec的specSize(View测量后的大小)
        case MeasureSpec.AT_MOST:
        case MeasureSpec.EXACTLY:
            result = specSize;
            break;
        }
        return result;
    }

注:View最终的大小是在layout阶段确定的(但几乎所有情况下View的测量大小和最终大小是相等的)

如果按照View的onMeasure()方法的默认实现,在布局中使用wrap_content就相当于使用match_parent。(因为在AT_MOST和EXACTLY模式下specSize都是parentSize,getDefaultSize()返回的测量宽高也都是specSize(parentSize),和match_parent的效果一样)
在自定义view时需要对wrap_content进行处理,比如TextView、ImageView等,系统都重写了它们的onMeasure()方法,不会这么简单直接拿specSize来当大小,而去会先去测量字符或者图片的高度等,然后拿到View本身content这个高度(字符高度等)。如果MeasureSpec是AT_MOST,而且View本身content的高度不超出MeasureSpec的size,那么可以直接用View本身content的高度(字符高度等)

View的measure过程总结

  • 从ViewGroup的measureChildWithMargins()开始,先计算View的MeasureSpec
  • 然后调用child.measure(),开始测量过程
  • measure()中调用onMeasure()
  • onMeasure()中确定测量宽高
    • 根据背景的最小尺寸和全局mMinWidth/Height确定默认值。
    • 根据specMode来决定最终尺寸是取默认值还是specSize。
  • onMeasure()确定测量宽高后通过set给全局变量mMeasuredWidth/Height

ViewGroup的measure过程

和View不同的是,它没有重写View的onMeasure方法,它提供了一个叫measureChildren的方法

为什么不像View一样对其onMeasure方法做统一的实现呢?

因为不同的ViewGroup子类有不同的布局特性(比如LinearLayout和RelativeLayout),这导致它们的测量细节各不相同

ViewGroup # measureChildren()

    protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
        final int size = mChildrenCount;
        final View[] children = mChildren;
        // 遍历子元素,调用measureChild()
        for (int i = 0; i < size; ++i) {
            final View child = children[i];
            if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
                measureChild(child, widthMeasureSpec, heightMeasureSpec);
            }
        }
    }

ViewGroup # measureChild()

和普通View测量MeasureSpec调用的方法差不多,也调用了getChildMeasureSpec()

    protected void measureChild(View child, int parentWidthMeasureSpec,
            int parentHeightMeasureSpec) {
        final LayoutParams lp = child.getLayoutParams();

        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
                mPaddingLeft + mPaddingRight, lp.width);
        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
                mPaddingTop + mPaddingBottom, lp.height);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }

获取测量宽高

在measure完成以后,通过getMeasuredWidth/Height方法就可以正确地获取到View的测量宽高(但在某些极端情况下,可能需要多次measure才能确定最终的测量宽高,所以在onMeasure方法中拿到的测量宽高是不准确的,可以在onLayout方法中获取)

如果我们要在Activity已启动的时候就获取某个View的宽高,怎么解决?

因为View的measure过程和Activity的生命周期方法不是同步执行的,因此无法保证Activity执行了onCreate、onStart、onResume时某个View已经测量完毕了(没有测量完毕,获得的宽高就是0)

1.Activity/View # onWindowFocusChanged

这个方法的含义是:View已经初始化完毕了,宽高已经准备好了。当Activity的窗口得到焦点和失去焦点时均会被调用一次,当频繁进行onResume()和onPause()时,这个方法就会频繁地被调用。

 @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            int width = view.getMeasuredWidth();
            int height = view.getMeasuredHeight();
        }
    }

2.view.post(runnable)

performTraversals也是通过向消息队列post调用这个方法的runnable来调用的,通过post可以将一个runnable投递到消息队列的尾部,然后等待Looper调用此runnable的时候,view也已经初始化好了

@Override
protected void onStart() {
    super.onStart();
    view.post(new Runnable() {
        @Override
        public void run() {
            int width = view.getMeasuredWidth();
            int height = view.getMeasuredHeight();
        }
    });
}

3.ViewTreeObserver

使用OnGlobalLayoutListener这个接口,当View树的状态发生改变或者View树内部的View的可见性发生改变时,OnGlobalLayout方法就会被回调,伴随着改变它会被调用多次。

    @Override
    protected void onStart() {
        super.onStart();

        ViewTreeObserver observer = view.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // 移除观察者,否则回调会多次执行
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                int width = view.getMeasuredWidth();
                int height = view.getMeasuredHeight();
            }
        });
    }

4.view.measure(int widthMeasureSpec, int heightMeasureSpec)

手动对View进行measure,这种方法比较复杂,要分情况处理,根据View的LayoutParams来分:

match_parent

无法测量,根据MeasureSpec的创建,当自身lp为match_parent时,specSize为parentSize,这种方法无法获取parentSize,所以不可行

具体数值
LayoutParams lp = view.getLayoutParams();
int widthSpec = View.MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
view.measure(widthSpec, heightSpec);
wrap_content

View的尺寸使用30位二进制表示,最大是30个1(即2^30-1),也就是(1<<30)-1,我们用View理论上能支持的最大值去构造MeasureSpec是合理的

int widthSpec = View.MeasureSpec.makeMeasureSpec((1 << 30) - 1, MeasureSpec.AT_MOST);
int heightSpec = View.MeasureSpec.makeMeasureSpec((1 << 30) - 1, MeasureSpec.AT_MOST);
view.measure(widthSpec, heightSpec);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值