Android --- Activity/Window/DecorView/ViewRootImpl的创建时机
自己看源码后的学习记录,仅供参考。
【Android】Activity/Window/DecorView/ViewRootImpl的创建时机
1.Activity 的创建时机
-> public static void main(String[] args)
-> ActivityThread.performLaunchActivity()// Activity 被创建的时候会调用 performLaunchActivity 方法
-> Activity activity = null;// 在 performLaunchActivity 方法内部,通过类加载器创建 Activity 实例
java.lang.ClassLoader cl = appContext.getClassLoader();
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
3.Window 的创建时机
-> ActivityThread.performLaunchActivity()
-> Activity.attach() // performLaunchActivity 方法内部调用了 attach
-> mWindow = new PhoneWindow(this, window, activityConfigCallback); // Activity 中的成员变量 mWindow
-> mDecor = (DecorView) preservedWindow.getDecorView();
4.DecorView 的创建时机
onCreate 中调用 setContentView 方法
AppcompatActivity.setContentView ->
AppCompatDelegateImpl.setContentView ->
AppCompatDelegateImpl.ensureSubDecor ->
AppCompatDelegateImpl.createSubDecor ->
Window.getDecorView ->
// Window 是个抽象类,如下:
// public abstract class Window
// PhoneWindow 是 Window 的实现类,如下:
// public class PhoneWindow extends Window implements MenuBuilder.Callback
// Window.getDecorView()也就是 PhoneWindow.getDecorView()
PhoneWindow.getDecorView ->
// 下面是 PhoneWindow 中的 getDecorView 方法
public class PhoneWindow extends Window implements MenuBuilder.Callback
@Override
public final @NonNull View getDecorView() {
if (mDecor == null || mForceDecorInstall) {
installDecor();
}
return mDecor;
}
// installDecor 方法中创建 DecorView 对象,并与 Window 绑定
-> installDecor()
-> mDecor = generateDecor(-1);
-> new DecorView(context, featureId, this, getAttributes());
-> mDecor.setWindow(this);
5.绘制流程:
->ActivityThread.handleResumeActivity()
->ViewManager.addView(decor, l);
// ViewManager是接口 ,如下:
// public interface ViewManager
// public interface WindowManager extends ViewManager
// WindowManager 还是接口
// public interface WindowManager extends ViewManager
// WindowManagerImpl 是 WindowManager 的实现,所以:
->WindowManager ->WindowManagerImpl ->WindowManagerGlobal 的addView()
ViewRootImpl是在WindowManagerGlobal的addView方法中被初始化的
并且也是在这里与DecorView进行绑定,成为DecorView的parent
-> addView()
-> root = new ViewRootImpl(view.getContext(), display);
view.setLayoutParams(wparams);
mViews.add(view);
mRoots.add(root);
mParams.add(wparams);
// do this last because it fires off messages to start doing things
try {
root.setView(view, wparams, panelParentView);
在ViewRootImpl的setView方法中,又会调用requestLayout,在这里就会进行这个View树的第一次测绘。
具体的方式是通过scheduleTraversals方法向Choreographer发送一个预定的消息,
并在下一次屏幕刷新的时候调用doTraversal → performTraversals方法进行ViewTree的测量、布局和绘制。
-> setView()
-> requestLayout()
-> scheduleTraversals()
-> mChoreographer.postCallback(
Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
-> doTraversal()
-> performTraversals()
-> performMeasure() / performLayout()/ performDraw()
ViewManager、WindowManager、WindowManagerImpl、WindowManagerGlobal之间的关系
public interface ViewManager
{
public void addView(View view, ViewGroup.LayoutParams params);
public void updateViewLayout(View view, ViewGroup.LayoutParams params);
public void removeView(View view);
}
public interface WindowManager extends ViewManager{
......
}
public final class WindowManagerImpl implements WindowManager {
private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();
@override
public void addView(view,params){
mGlobal.addView(...);
}
public void updateViewLayout(view,params){
mGlobal.updateViewLayout(...);
}
public void remove(view){
mGlobal.remove(...);
}
}
public final class WindowManagerGlobal {
public void addView(...){
......
}
public void updateViewLayout(...) {
......
}
public void removeView(...) {
......
}
}