Android视图加载流程(3)之ViewRootImpl的UI刷新机制
从上一篇文章,大家应该对UI的绘制流程有一个大概的了解。视图的绘制工作大概分为Measure、Layout和Draw三个主要的流程。而启动这些流程的一个入口则是上一篇文章里ViewRootImpl类的performTraversals()。
private void performTraversals() {
......
//最外层的根视图的widthMeasureSpec和heightMeasureSpec由来
//lp.width和lp.height在创建ViewGroup实例时等于MATCH_PARENT
int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
......
mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
......
mView.layout(0, 0, mView.getMeasuredWidth(), mView.getMeasuredHeight());
......
mView.draw(canvas);
......
}
该函数做的执行过程主要是根据之前设置的状态,判断是否重新计算视图大小(measure)、是否重新放置视图的位置(layout)、以及是否重绘 (draw),其核心也就是通过判断来选择顺序执行这三个方法中的哪个。
理解图 本文重点Measure
源码解读
Step1:ViewRootImpl
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.
//Window无法调整大小,强制根视图的宽高为Window的宽高,即全屏
measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
break;
......
}
return measureSpec;
}
由注释得此MeasureSpec是测量RootView的。由于默认的rootDimension为MATCH_PARENT,所以switch走的是MATCH_PARENT,然后使用MeasureSpec.makeMeasureSpec
方法组装一个MeasureSpec,specMode=EXACTLY,specSize=WindowSize,也就是为何根视图是全屏的原因。
Step2:View
//final方法,子类不可重写
public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
......
//回调onMeasure()方法
onMeasure(widthMeasureSpec, heightMeasureSpec);
......
}
measure为整个View树计算实际的大小,然后设置实际的宽高。每个View空间的实际宽高都是由父视图和自身决定的。由于measure为final,View的子类无法重写该方法,因此View子类只能通过重写onMeasure实现自己的测量逻辑,的measure方法最终回调了View的onMeasure方法。
measure传入了两个重要测量参数MeasureSpce,详情理解请进传送门
Step3:View
//View的onMeasure默认实现方法
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
真正设置View的宽高的即setMeasuredDimension
方法,它对View的成员变量mMeasuredWidth和mMeasuredHeight变量赋值。如果想使用现成的View想通过getMeasuredWidth()和getMeasuredHeight()方法来获取View测量的宽高,必须保证这两个方法在onMeasure流程之后被调用才能返回有效值。
如果不调用此方法的话会出现异常!
xxView#onMeasure() did not set the measured dimension by calling setMeasuredDimension()
默认系统会给我们测量一个默认的宽高,具体如下~
//获取建议的最小宽度
protected int getSuggestedMinimumWidth() {
return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
}
//获取建议的最小高度
protected int getSuggestedMinimumHeight() {
return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
}
建议的最小宽度和高度都是由View的Background尺寸与通过设置View的miniXXX属性共同决定的。
public static int getDefaultSize(int size, int measureSpec) {
int result = size;
//通过MeasureSpec解析获取mode与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;
}
如果specMode等于AT_MOST或EXACTLY就返回specSize,这就是系统默认的规格。
Step4 View
到这里一次简单的View视图Measure已经完成。DecorView作为根视图,系统默认设置全屏显示。而DecorView也是一个父视图,那DecorView如何测量自己的所有子视图呢?
此时有3个重要的方法measureChildren
, measureChild
, measureChildWithMargins
- measureChildren内部循环调用measureChild
- measureChild和measureChildWithMargins内部调用了子视图的measure,而两者的区别就在于是否要将margin作子自视图的大小
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);
}
}
}
protected void measureChild(View child, int parentWidthMeasureSpec,
int parentHeightMeasureSpec) {
//获取子视图的LayoutParams
final LayoutParams lp = child.getLayoutParams();
//区别之处:第二入参
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
mPaddingLeft + mPaddingRight, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
mPaddingTop + mPaddingBottom, lp.height);
//调运子View的measure方法,子View的measure中会回调子View的onMeasure方法
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
protected void measureChildWithMargins(View child,
int parentWidthMeasureSpec, int widthUsed,
int parentHeightMeasureSpec, int heightUsed) {
//获取子视图的LayoutParams
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);
//调运子View的measure方法,子View的measure中会回调子View的onMeasure方法
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
两个方法就是对父视图提供的measureSpec参数结合自身的LayoutParams参数进行了调整,然后再来调用child.measure()方法,具体通过方法getChildMeasureSpec来进行参数调整。
Step5 View
public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
//获取当前Parent View的Mode和Size
int specMode = MeasureSpec.getMode(spec);
int specSize = MeasureSpec.getSize(spec);
//获取Parent size与padding差值(也就是Parent剩余大小),若差值小于0直接返回0
int size = Math.max(0, specSize - padding);
//定义返回值存储变量
int resultSize = 0;
int resultMode = 0;
//依据当前Parent的Mode进行switch分支逻辑
switch (specMode) {
// Parent has imposed an exact size on us
//默认Root View的Mode就是EXACTLY
case MeasureSpec.EXACTLY:
if (childDimension >= 0) {
//如果child的layout_widh属性在xml或者java中给予具体大于等于0的数值
//设置child的size为真实layout_widh属性值,mode为EXACTLY
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
//如果child的layout_widh属性在xml或者java中给予MATCH_PARENT
// Child wants to be our size. So be it.
//设置child的size为size,mode为EXACTLY
resultSize = size;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
//如果child的layout_widh属性在xml或者java中给予WRAP_CONTENT
//设置child的size为size,mode为AT_MOST
// Child wants to determine its own size. It can't be
// bigger than us.
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break;
......
//其他Mode分支类似
}
//将mode与size通过MeasureSpec方法整合为32位整数返回
return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
}
getChildMeasureSpec的逻辑是通过将父视图提供的MeasureSpec参数得到spceMode和spectSize,然后通过计算出来的size以及子View的childDimension(layout_width或layout_height)来计算出ChildMeasureSpec,子视图通过ChildMeasureSpec调用measure函数,计算子视图的宽高。
疑问 wonder
循环测量子视图的measureChildren方法是在哪个时候调用呢?
此方法是比较少调用的,大部分的ViewGroup都有自己的特殊实现(基本都在onMeasure里实现)。我们以DecorView【FrameLayout】为例:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
...
//设置DecorView的宽高
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
resolveSizeAndState(maxHeight, heightMeasureSpec,
childState << MEASURED_HEIGHT_STATE_SHIFT));
//for循环设置DecorView子视图的宽高
count = mMatchParentChildren.size();
if (count > 1) {
for (int i = 0; i < count; i++) {
...
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
}
}
总结 Summary
通过以上几个步骤的分析,测量(Measure)是从顶层父View向子View地柜调用view.measure的方法。
- MeasureSpec(View的内部类)作为测量视图的重要数据,不仅包含了size还有mode,详细了解请进传送门
- 顶层DecorView所需的MeasureSpec是由ViewRootImpl中的getRootMeasureSpect提供的(LayoutParams宽高参数均为MATCH_PARENT,specMode是EXACTLY,specSize为物理屏幕大小)
- ViewGroup类提供了measureChild,measureChild和measureChildWithMargins方法,简化了父子View的尺寸计算
- View的布局大小由父View和子View共同决定。
- 使用View的getMeasuredWidth()和getMeasuredHeight()方法来获取View测量的宽高,必须保证这两个方法在onMeasure流程之后被调用才能返回有效值。
Android视图加载流程(5)之View的详细绘制流程Layout
PS:本文
整理
自以下文章,若有发现问题请致邮 caoyanglee92@gmail.com
网友评论