View绘制流程
相关对象介绍
ViewRootImpl
- 连接WindowManager 和 DecorView
- 完成View绘制的三大流程: measure、layout、draw
- implements ViewParent
- 分发底层传递的行为事件
DecorView
- Android 视图树的根节点,同时也是 FrameLayout 的子类
setContentView
- Activity setContentView 调用了PhoneWindow的setContentView()
- PhoneWindow的setContentView() 调用->installDecor() 调用->generateDecor()-> new DecorView()
流程
- ActivityThread的performLaunchActivity()调用 activity.attach()
- Activity的attach() 初始化了PhoneWindow和WindowManager new PhoneWindow setWindowManager()
- ActivityThread执行mInstrumentation.callActivityOnCreate()方法,然后执行Activity.onCreate(),再执行Activity setContentView
- Activity setContentView 调用了PhoneWindow的setContentView()
- PhoneWindow的setContentView() 调用->installDecor() 调用->generateDecor()-> new DecorView()
- 此时 DecorView PhoneWindow 都已经创建
- ActivityThread的handleResumeActivity() 调用WindowManagerImpl 的addView()
- WindowManagerImpl 的addView 实际是调用WindowManagerGlobal addView() , 在addView WindowManagerGlobal 中创建ViewRootImpl, 并将DecorView传入ViewRootImpl,完成windowManagerService和View的连接
- ViewRootImpl performTraversal()->performMeasure() performLayout() performDraw
//===========ActivityThread.java==========
final void handleResumeActivity(...) {
......
//跟踪代码后发现其初始赋值为mWindow = new PhoneWindow(this, window, activityConfigCallback);
r.window = r.activity.getWindow();
//从PhoneWindow实例中获取DecorView
View decor = r.window.getDecorView();
......
//跟踪代码后发现,vm值为上述PhoneWindow实例中获取的WindowManager。
ViewManager wm = a.getWindowManager();
......
//当前window的属性,从代码跟踪来看是PhoneWindow窗口的属性
WindowManager.LayoutParams l = r.window.getAttributes();
......
wm.addView(decor, l);
......
}
root = new ViewRootImpl(view.getContext(), display);
root.setView(view, wparams, panelParentView);
// =====================ViewRootImpl.java=================
private void performTraversals() {
......
int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
......
// Ask host how big it wants to be
performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
......
performLayout(lp, mWidth, mHeight);
......
performDraw();
}
View WindowManager Window关系图
View的绘制