Android View的绘制流程三部曲

本文详细探讨了Android中View的绘制流程,包括measure过程、layout过程和draw过程。measure阶段涉及MeasureSpec的计算,确定View的大小;layout阶段布局View在父容器中的位置;draw阶段则负责绘制View及其子元素。通过对源码的分析,揭示了ViewGroup如何处理子View的测量和布局,以及View自身如何进行绘制。
摘要由CSDN通过智能技术生成

在刚开始学习Java的时候,我看的是Mars老师的视频。Mars老师说过的一句话让我印象很深刻:要有一颗面向对象的心。

如果我们用面向对象的思维方式来思考,就会觉的View的绘制机制是很合理,很科学的。我们要在一张纸上画一幅画,首先要测量一下这幅画有多大吧,然后确定在这张纸的哪个地方画会显得比较美观,最后才是用画笔工具将画绘制在纸上。
在Android中也是一样的。View的绘制流程主要是指measure,layout,draw这三步,即测量,布局,绘制。首先是要测量View的宽高,然后布局确定其在父容器中的位置坐标,最后才是绘制显示出来。那这篇博客就一起来探索View的绘制流程吧。

View的绘制流程从ViewRootImpl的performTraversals方法开始,在performTraversals方法中会调用performMeasure、performLayout、performDraw三个方法来遍历完成整棵视图树的绘制。

measure过程

MeasureSpec

performMeasure方法是这样被调用的:

performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);

接收了两个参数,很好奇这两个参数是什么。看名字是“子View宽测量说明书”和“子View高测量说明书”?应该先来了解一下MeasureSpec。

MeasureSpec是一个32位的int值,高2位是specMode记录的是测量模式,低30位是specSize记录的是测量大小。

specMode有三种类型:

EXACTLY : 精确值模式,表示父视图希望子视图的大小应该是由specSize的值来决定的,这个时候View的最终大小就是specSize所记录的大小。对应于LayoutParams中的 match_parent和具体数值这两种模式。比如 android:layout_width=”match_parent”,android:layout_width=”50dp”

AT_MOST : 最大值模式,表示父容器指定了一个可用大小specSize,子视图最多只能是specSize中指定的大小,不能大于这个值。对应于LayoutParams中的 wrap_content的形式。

UNSPECIFIED :父容器不对View有任何限制,View想多大就多大,一般不会用到

MeasureSpec到底是用来干嘛的?
系统是通过View的MeasureSpec来确定View的测量宽高的

MeasureSpec是怎么来的?
对于普通的View来说,其MeasureSpec由父容器的MeasureSpec和自身的LayoutParams共同确定。对于顶级View(DecorView),其MeasureSpec由窗口的尺寸和其自身的LayoutParams共同确定。

我们回到performMeasure方法,来看看传入的参数childWidthMeasureSpec和childHeightMeasureSpec,这两个MeasureSpec是顶级View的,它们由窗口的尺寸和其自身的LayoutParams共同确定。那它们又是怎么产生的?在ViewRootImpl的measureHierarchy方法中,有两行代码是这样的:

childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);

getRootMeasureSpec方法获取到根View(DecorView)的MeasureSpec。传入的参数desiredWindowWidth和desiredWindowHeight是屏幕的尺寸。lp.width 和lp.height都是MATCH_PARENT。

那么探探getRootMeasureSpec方法,如下:

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

会转到MeasureSpec的makeMeasureSpec方法,而makeMeasureSpec方法就是将SpecSize和SpecMode包装成32位的int值。
那makeMeasureSpec方法是怎么组装MeasureSpec的呢?如下:

public static int makeMeasureSpec(@IntRange(from = 0, to = (1 << MeasureSpec.MODE_SHIFT) - 1) int size,
                                  @MeasureSpecMode int mode) {
    if (sUseBrokenMakeMeasureSpec) {
        return size + mode;
    } else {
        return (size & ~MODE_MASK) | (mode & MODE_MASK);
    }
}

这时,根View的MeasureSpec就诞生了。它将参与构成子元素的MeasureSpec。

而对于普通的View,其MeasureSpec由父容器的MeasureSpec和自身的LayoutParams共同构成。我们知道刚才getRootMeasureSpec方法获取到的是顶级View的MeasureSpec,顶级View本身就是父容器。
那现在看看ViewGroup的measureChildWithMargins方法,这个方法是用来测量子View的。如下:

protected void measureChildWithMargins(View child,
        int parentWidthMeasureSpec, int widthUsed,
        int parentHeightMeasureSpec, int heightUsed) {
    final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

    final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
            mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
                    + widthUsed, lp.width);
    final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
            mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
                    + heightUsed, lp.height);

    child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}

首先是调用子元素的getLayoutParams方法获取到子元素的LayoutParams,之后调用了getChildMeasureSpec方法来获取到子元素的MeasureSpec,可以看到传入了父元素的MeasureSpec。

getChildMeasureSpec方法很重要,能让我们了解子元素MeasureSpec的产生过程,如下:

public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值