美文网首页
View测量流程

View测量流程

作者: moodstrikerdd | 来源:发表于2019-04-03 16:45 被阅读0次

    View测量流程简介

    ViewGroup继承自View,在View的测量方法measure方法中,调用了onMeasure,onMeasure是View具体实现测量过程的方法,也是自定义View时需要自己去实现的方法。
    对于一个activity来说测量流程是子上而下的:
    1.ViewRootImpl 调用performMeasure 测量Activity 顶层View DecorView,决定其测量模式。
    2.ViewGroup在onMeasure中调用measureChildrenchild.measure测量出子View的大小。
    3.View在onMeasure中实现对自身大小的测量,并调用setMeasuredDimension设置具体宽高尺寸。

    MeasureSpec

    MeasureSpec是View测量中很重要的一个类,由mode和size两部分组成。mode表示父View对其子View大小的约束,size则是具体尺寸。mode+size对应最终MeasureSpec的值。

    mode 对子View的约束
    MeasureSpec.UNSPECIFIED 对子View大小无任何约束,子View可为任意大小
    MeasureSpec.EXACTLY 确切的大小,如:100dp或者march_parent
    MeasureSpec.AT_MOST 子View可以达到其想要达到的大小,如:wrap_content

    MeasureSpec.makeMeasureSpec(size,mode)可以得到int类型的MeasureSpec的值,在测量过程中measure onMeasure都是通过此值来进行View的测量。

    View测量起点ViewRoot

    ViewRoot是View绘制的起点,实现类ViewRootImpl中会在performTraversals中调用performMeasureperformLayoutperformDraw分别开始View的测量,布局,绘制。

     int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
     int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
     performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
    

    在performTraversals调用performMeasure时,传入了两个参数,所测量View(此处为DecorView)的宽高MeasureSpec,而这两个参数都是有getRootMeasureSpec方法返回,在getRootMeasureSpec接受了两个参数,windowSize(一般为屏幕尺寸),rootDimension(DecorView的宽高属性)

     private static int getRootMeasureSpec(int windowSize, int rootDimension) {
            int measureSpec;
            switch (rootDimension) {
    
            case ViewGroup.LayoutParams.MATCH_PARENT:
                // Window can't resize. Force root view to be windowSize.
                measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
                break;
            case ViewGroup.LayoutParams.WRAP_CONTENT:
                // Window can resize. Set max size for root view.
                measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
                break;
            default:
                // Window wants to be an exact size. Force root view to be that size.
                measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
                break;
            }
            return measureSpec;
        }
    

    在getRootMeasureSpec方法中可以看出:
    1.当DecorView的宽或者高为MATCH_PARENT的时候,DecorView的测量模式为EXACTLY,size为window的size。
    2.当DecorView的宽或者高为WRAP_CONTENT的时候,DecorView的测量模式为AT_MOST,size为window的size。
    3.其他情况,即宽高为指定大小的值,如100dp的时候,DecorView的测量模式为EXACTLY,size为具体的值。

    private void performMeasure(int childWidthMeasureSpec, int childHeightMeasureSpec) {
            if (mView == null) {
                return;
            }
            Trace.traceBegin(Trace.TRACE_TAG_VIEW, "measure");
            try {
                mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
            } finally {
                Trace.traceEnd(Trace.TRACE_TAG_VIEW);
            }
        }
    

    在performMeasure方法中调用了mView.measure,此处的mView就是DecorView,DecorView继承自FrameLayout,则具体测量任务交给ViewGroup处理。

    ViewGroup测量

    ViewGroup继承自View,但是并没有具体onMeasure实现,因为ViewGroup中是可以包含View的,当ViewGroup的款或者高为WRAP_CONTENT的时候,对于不同的ViewGroup采用的方法不同,如RelativeLayout和LinearLayout。不过VIewGroup提供了默认实现measureChildrenmeasureChildmeasureChildWithMargins来测量子VIew的大小。

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

    在measureChildren中遍历了ViewGroup中的View,并调用measureChild对非GONE的View进行测量。

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

    measureChild中则是先获取了子View的Width和heigth,然后调用了子View的measure方法。

    public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
            int specMode = MeasureSpec.getMode(spec);
            int specSize = MeasureSpec.getSize(spec);
    
            int size = Math.max(0, specSize - padding);
    
            int resultSize = 0;
            int resultMode = 0;
    
            switch (specMode) {
            // Parent has imposed an exact size on us
            case MeasureSpec.EXACTLY:
                if (childDimension >= 0) {
                    resultSize = childDimension;
                    resultMode = MeasureSpec.EXACTLY;
                } else if (childDimension == LayoutParams.MATCH_PARENT) {
                    // Child wants to be our size. So be it.
                    resultSize = size;
                    resultMode = MeasureSpec.EXACTLY;
                } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                    // Child wants to determine its own size. It can't be
                    // bigger than us.
                    resultSize = size;
                    resultMode = MeasureSpec.AT_MOST;
                }
                break;
    
            // Parent has imposed a maximum size on us
            case MeasureSpec.AT_MOST:
                if (childDimension >= 0) {
                    // Child wants a specific size... so be it
                    resultSize = childDimension;
                    resultMode = MeasureSpec.EXACTLY;
                } else if (childDimension == LayoutParams.MATCH_PARENT) {
                    // Child wants to be our size, but our size is not fixed.
                    // Constrain child to not be bigger than us.
                    resultSize = size;
                    resultMode = MeasureSpec.AT_MOST;
                } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                    // Child wants to determine its own size. It can't be
                    // bigger than us.
                    resultSize = size;
                    resultMode = MeasureSpec.AT_MOST;
                }
                break;
    
            // Parent asked to see how big we want to be
            case MeasureSpec.UNSPECIFIED:
                if (childDimension >= 0) {
                    // Child wants a specific size... let him have it
                    resultSize = childDimension;
                    resultMode = MeasureSpec.EXACTLY;
                } else if (childDimension == LayoutParams.MATCH_PARENT) {
                    // Child wants to be our size... find out how big it should
                    // be
                    resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
                    resultMode = MeasureSpec.UNSPECIFIED;
                } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                    // Child wants to determine its own size.... find out how
                    // big it should be
                    resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
                    resultMode = MeasureSpec.UNSPECIFIED;
                }
                break;
            }
            //noinspection ResourceType
            return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
        }
    

    在getChildMeasureSpec方法中会根据ViewGroup的测量模式和子view的具体宽高来决定子View的MeasureSpec。具体对应关系如下


    View测量

    View是测绘的最底层,一般自定义View的时候都需要重写onMeasure方法,当不去重写且View设置了WRAP_CONTENT的时候,在实际展示效果中却是填充了整个该View所在的ViewGroup。查看View onMeasure的默认实现方法可以知道原因。

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            setMeasuredDimension(
                    getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                    getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
    }
    
    protected int getSuggestedMinimumWidth() {
            return (mBackground == null) ? 
                      mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
    }
    

    View的onMeasure中直接根据ViewGroup传递过来的width和heigth获取了默认的宽高并设置给View。

    public static int getDefaultSize(int size, int measureSpec) {
            int result = size;
            int specMode = MeasureSpec.getMode(measureSpec);
            int specSize = MeasureSpec.getSize(measureSpec);
    
            switch (specMode) {
            case MeasureSpec.UNSPECIFIED:
                result = size;
                break;
            case MeasureSpec.AT_MOST:
            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
            }
            return result;
        }
    

    在getDefaultSize中可以看到当View的测量模式为UNSPECIFIED的时候返回的是View设置的最小尺寸,而当测量模式为AT_MOST和EXACTLY的时候,返回的都是ViewGroup传过来的尺寸。根据源码和ViewGroup View测量模式对应关系可知道,出现上述问题的情况有3中情况
    1.ViewGroup的测量模式为EXACTLY,View的宽或者高为wrap_content。
    2.ViewGroup的测量模式为AT_MOST,View的宽或者高为match_parent。
    3.ViewGroup的测量模式为AT_MOST,View的宽或者高为wrap_content。

    相关文章

      网友评论

          本文标题:View测量流程

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