android源代码量,AndroidUI绘制流程,一步一步深入源码解析(三)view测量上篇

view的测量

根据(二)中分析,我们知道view的测量、布局、绘制分别调用了performMeasure、performLayout、perfromDraw方法;

源码如下:

121f82ee394f

121f82ee394f

121f82ee394f

总结UI详细测量、布局和绘制步骤如下:

1、测量:

View.measure--->View.onMeasure--->View.setMeasuredDimension--->View.setMeasureDimensionRaw

2、布局:

View.layout ---> View.onLayout

3、绘制:

ViewRootImpl.draw(fullRedrawNeeded)--->ViewRootImpl.drawSoftware--->view.draw(Canvas)

下面我们依次分析具体的测量、布局和绘制实现逻辑

测量:

View的测量会执行measure(int widthMeasureSpec, int heightMeasureSpec)方法,至于measure怎么执行先不探讨,我们先了解一下其中的两个参数,其实他们都是int类型的MeasureSpec。MeasureSpec可以说是View测量过程的前提,所以我们很有必要先来了解一下MeasureSpec。

MeasureSpec 工作原理

MeasureSpec 代表一个32位的int值,高2位代表SpecMode,低30位代表SpecSize。

SpecMode是指测量模式,SpecSize是指在某种测量模式下的大小。

MeasureSpec是View中的一个静态内部类。

121f82ee394f

我们可以把MeasureSpec理解为测量规则,而这个测量规则是由测量模式和和该模式下的测量大小共同组成的。

121f82ee394f

使用方式如下:

int MeasureSpec = MeasureSpec.makeMeasureSpec(specSize,SpecMode);

确定了View测量规则后,我们也可以通过测量规则获取测量模式和该模式下的测量大小。

int specMode = MeasureSpec.getMode(measureSpec);

int specSize = MeasureSpec.getSize(measureSpec);

SpecMode有三类:

UNSPECIFIED

父容器不对View有任何限制,要多大给多大,这般情况一般用于系统内部,表示一种测量状态,如ScrollView测量子View时用的就是这个。

EXACTLY

父容器已经检测出View所需要的大小,这个时候View的最终大小就是SpecSize所测定的值,它对应于LayoutParams中的match_parent和具体的数值(如40dp,60dp)这两种模式。

AT_MOST

父容器指定了一个可用大小即SpecSize,View的大小不能大于这个值,具体是什么值要看不同View的具体实现。它对应于LayoutParams中的wrap_content.

下面我们再详细分解普通View如何测量

普通View的MeasureSpec的创建过程

MeasureSpec很重要,上文中我们也了解了MeasureSpec的工作原理,那如何获取MeasureSpec呢?下面就结合源码来分析MeasureSpec的创建过程。

先来看下ViewGroup中的measureChild方法

121f82ee394f

在这个方法中,先获取了子View的布局参数,然后通过getChildMeasureSpec方法分别得到子View的宽高测量规则,即childWidthMeasureSpec和childHeightMeasureSpec,最后调用子View的measure方法,至此测量过程就由父View传递到了子View.。MeasureSpec确定后就可以在onMeasure方法确定View的测量宽高了。

下面重点分析一下getChildMeasureSpec,源码如下:

/**

* Does the hard part of measureChildren: figuring out the MeasureSpec to

* pass to a particular child. This method figures out the right MeasureSpec

* for one dimension (height or width) of one child view.

*

* The goal is to combine information from our MeasureSpec with the

* LayoutParams of the child to get the best possible results. For example,

* if the this view knows its size (because its MeasureSpec has a mode of

* EXACTLY), and the child has indicated in its LayoutParams that it wants

* to be the same size as the parent, the parent should ask the child to

* layout given an exact size.

*

* @param spec The requirements for this view

* @param padding The padding of this view for the current dimension and

*        margins, if applicable

* @param childDimension How big the child wants to be in the current

*        dimension

* @return a MeasureSpec integer for the child

*/

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

}

此方法比较清晰,它主要用来通过父View的MeasureSpec和子View的LayoutParams来确定子View的MeasureSpec的,即普通View的MeasureSpec创建过程。

121f82ee394f

接着我们再了解一下DecorView的MeaureSpace是如何创建的

普通View的MeasureSpec的创建过程阐述了怎样通过父View的MeasureSpec和子View的LayoutParams来确定子View的MeasureSpec。那顶级View,即DecorView的MeasureSpec创建过程又是怎样的呢?ViewRootImp的measureHierarchy方法中有如下代码:

121f82ee394f

接着来看getRootMeasureSpec方法

121f82ee394f

从上述源码,我们可以得出如下规则,具体根据它的LayoutParams来划分:

LayoutParams.MATCH_PARENT:精确模式 其大小就为屏幕的尺寸大小

ViewGroup.LayoutParams.WRAP_CONTENT:最大模式,大小不定,但是不能超过屏幕的大小

具体数值(如40dp):精确模式,大小为LayoutParamas指定的大小。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值