众所周知View树的绘制入口在ViewRootImpl. performTraversals()方法。这里只挑重点看流程,不纠结于Measure()、layout()、draw()的详细过程。
private void performTraversals() {
//测量
performMeasure(...);
//布局
performLayout(...);
//绘制
performDraw();
}
- performMeasure()
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);
}
}
View显示时调用到ViewRootImpl.setView()
public void setView(View view...){
mView = view;
}
mView就是包含content的DecorView
public final void measure(...){
onMeasure(widthMeasureSpec, heightMeasureSpec);
}
DecorView继承自FrameLayout重写了onMeasure(),DecorView.onMeasure()
protected void onMeasure(...){
...
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
调用到父类FrameLayout.onMeasure()
protected void onMeasure(...){
...
for (int i = 0; i < count; i++) {
...
//测量子View
measureChildWithMargins()
}
//设置大小
setMeasuredDimension(...)
for (int i = 0; i < count; i++) {
...
//遍历子view,调用measure()完成View树测量
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
...
}
ViewGroup.measureChildWithMargins()
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);
}
ViewRootImpl.performMeasure()调用到DecorView.onMeasure();DecorView继承自FrameLayout也就是Viewgroup,遍历子View调用子View.Measure(),内部调用onMeasure()完成测量。
- performLayout()
private void performLayout(){
final View host = mView;
...
host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
}
DecorView.onLayout()
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
...
}
FrameLayout.onLayout()
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
layoutChildren(left, top, right, bottom, false /* no force left gravity */);
}
FrameLayout.layoutChildren()
void layoutChildren(...){
...
child.layout(...)
}
View.layout()确定自身位置。ViewRootImpl.performLayout()调用到DecorView.onLayout()。ViewGroup.onLayout()是抽象方法,实现类必需重写。ViewGroup需先遍历子View,调用View.onLayout()确定子View位置,然后才能确定自身位置。子View如果是View不是ViewGroup,其onLayou()是空方法,因为View不会有子View。
- performDraw()
private void performDraw() {
boolean canUseAsync = draw(fullRedrawNeeded);
}
ViewRootImpl.draw()
private boolean draw(...){
if (!drawSoftware(surface, mAttachInfo, xOffset, yOffset,
scalingRequired, dirty, surfaceInsets)) {
return false;
}
}
ViewRootImpl.drawSoftware()
private boolean drawSoftware(...){
mView.draw(canvas);
}
DecorView.draw()
public void draw(Canvas canvas) {
super.draw(canvas);
if (mMenuBackground != null) {
mMenuBackground.draw(canvas);
}
}
super.draw(canvas)调用到View.draw()
public void draw(Canvas canvas) {
...
//背景
drawBackground(canvas);
//绘制
onDraw(canvas)
//遍历调用子View.draw()
dispatchDraw(canvas);
//前景
onDrawForeground(canvas);
//默认焦点
drawDefaultFocusHighlight(canvas)
}
View.dispatchDraw()是空方法,因为View不会有子View
protected void dispatchDraw(Canvas canvas) {
}
ViewGroup.dispatchDraw()遍历子View调用ViewGroup.drawChild(),间接调用子View.draw()
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
return child.draw(canvas, this, drawingTime);
}
ViewRootImpl.performDraw()方法调用到DecorView.draw()。draw()方法中绘制背景、调用onDraw()方法(DecorView是ViewGroup,自身并没有内容,所以没有重写onDraw(),其父类View.onDraw()也是空方法,也就是说ViewGroup不用绘制自身)、遍历调用子View.draw()、绘制前景和默认焦点,直至View树绘制完成。
总结下:setContentView()方法通过ViewRootImpl. performTraversals()开启绘制流程。该方法内部连续调用performMeasure()、performLayout()、performDraw(),最终对应调用到View的onMeasure()、onLayout()、onDraw()。布局解析出来后是树形结构,顶层View是DecorView这个继承自FrameLayout的ViewGroup。而ViewGroup需要遍历所有子View,对应调用三个方法,直至整个View树绘制完毕。不同的是ViewGroup是容器,自身并不需要绘制,所以onDraw()是空方法;View的位置由其父View->ViewGroup决定,自身没有子View,所以不需要onLayout()。