源码读起来老是容易忘掉,备忘一下记号。decorView , Window 是通过ViewRooImp来建立联系的,那么他们分别的创建时间点是在哪?看了这个忘了那个。。。。
ActivityThread.java 中
performLaunchActivity() 创建Activity()
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
activity调用Activity中的attach创建Window
mWindow = new PhoneWindow(this, window);
handleResumeActivity()调用Phonewindow 的getDecorView()创建DecorView()
View decor = r.window.getDecorView();
@Override
public final View getDecorView() {
if (mDecor == null || mForceDecorInstall) {
installDecor();
}
return mDecor;
}
installDecor中newDecorView
继续调用addview
wm.addView(decor, l);
WindowManagerGlobal.java 中的addView创建
root = new ViewRootImpl(view.getContext(), display);
mViews.add(view);
mRoots.add(root);
mParams.add(wparams);
添加到所有根列表,1)mViews所有DecorView 2)mRoots所有ViewRootImpl 3)DecorView的WindowParam。 便于之后的查找删除更新。
ViewRootImpl.java
创建完之后setView()
root.setView(view, wparams, panelParentView);
在这里会调用绘制流程。
// Schedule the first layout -before- adding to the window
// manager, to make sure we do the relayout before receiving
// any other events from the system.
requestLayout();
这里说明,第一次添加Window。
进入到performTraversals()
res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
getHostVisibility(), mDisplay.getDisplayId(),
mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
mAttachInfo.mOutsets, mInputChannel);
这其中的参数mWindow 是一个Stub也就是个Binder,类中持用ViewRooImpl的弱引用。
mViewAncestor = new WeakReference<ViewRootImpl>(viewAncestor);
添加到界面显示,在Session.java
@Override
public int addToDisplay(IWindow window, int seq, WindowManager.LayoutParams attrs,
int viewVisibility, int displayId, Rect outContentInsets, Rect outStableInsets,
Rect outOutsets, InputChannel outInputChannel) {
return mService.addWindow(this, window, seq, attrs, viewVisibility, displayId,
outContentInsets, outStableInsets, outOutsets, outInputChannel);
}
之后调用一系列将Window添加到显示的界面,还有一些界面相关的类,和底层显示相关的就没有看了,书上也没有介绍,和应用开发联系不大。
综上来看,ViewRootImple,连接了Window,DecorView,并且使用Binder的调用将Window添加,至于如何添加不再关注,因为有很多底层的绘制和类要分析且没有必要。