原文 Android中的ViewRootImpl类源码解析 http://blog.csdn.net/qianhaifeng2012/article/details/51737370
这篇文章是对上面文章部分内容的总结:
ViewRoot和ViewRootImpl,从字面上来看,还以为ViewRootImpl是ViewRoot的实现, 其实不是的。ViewRoot是Android2.2以前的,2.2之后就被ViewRootImpl替代了。
ViewRootImpl的作用:
1,WindowManager和DecorView的纽带
2,绘制View,调用了performMeasure(), performLayout(), performDraw()方法,因而调用View的 measure,layout,draw方法完成view的绘制。
3,传递各种event事件,如按键,触屏等事件给View。
ViewRootImpl
ViewRootImpl是一支树结构的顶部,但是不是View, 实现了View和WindowManager的通信协议,大部分的实现细节是在WindowManagerGlobal类中的。
WindowManager
WindowManager主要有增、删、改view的 三个方法,并且这 三个方法是通过继承ViewManager获得的。并且这三个方法是由WindowManager的实现类WindowManagerImpl实现的,而WindowManagerImpl对三个方法的实现是通过调用WindowManagerGlobal的具体方法来实现的。
WindowManagerGlobal
不能直接访问Window,需要通过WindowManger访问Window,而WindowManagerImpl实现WindowManager,具体的实现里又调用了WindowManagerGlobal的 方法,比如,想添加Window,最终调用的是WindowManagerGlobal的addView方法,添加一个Window其实就是在添加一个View,Window只是一个抽象的概念。在WindowManagerGlobal中有下面几个成员变量
private final ArrayList<View> mViews = new ArrayList<View>();
private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();
private final ArrayList<WindowManager.LayoutParams> mParams =
new ArrayList<WindowManager.LayoutParams>();
private final ArraySet<View> mDyingViews = new ArraySet<View>();
mViews存储的是所有Window所对应的View,mRoots存储的是所有Window所对应的ViewRootImpl,mParams存储的是所有Window所对应的布局参数,而mDyingViews存储了那些正在被删除的View对象,或者说是那些已经调用removeView方法但是还没有删除的Window对象。
WindowManagerGlobal的addView方法:就是用来 添加Window的
public void addView(View view, ViewGroup.LayoutParams params,
Display display, Window parentWindow) {
//这里都是一些参数的判断和设置,省略掉了
...
ViewRootImpl root;
View panelParentView = null;
synchronized (mLock) {
// Start watching for system property changes.
...
int index = findViewLocked(view, false);
if (index >= 0) {
if (mDyingViews.contains(view)) {
// Don't wait for MSG_DIE to make it's way through root's queue.
mRoots.get(index).doDie();
} else {
throw new IllegalStateException("View " + view
+ " has already been added to the window manager.");
}
// The previous removeView() had not completed executing. Now it has.
}
// If this is a panel window, then find the window it is being
// attached to for future reference.
...
//在这里初始化ViewRootImpl
root = new ViewRootImpl(view.getContext(), display);
//给要添加的View设置参数
view.setLayoutParams(wparams);
//把要添加的View存入集合
mViews.add(view);
//把初始化的ViewRootImpl存入集合
mRoots.add(root);
mParams.add(wparams);
}
// do this last because it fires off messages to start doing things
try {
//从这里可以看出,真正添加View(就是添加Window)的其实就是ViewRootImpl了
root.setView(view, wparams, panelParentView);
} catch (RuntimeException e) {
// BadTokenException or InvalidDisplayException, clean up.
synchronized (mLock) {
final int index = findViewLocked(view, false);
if (index >= 0) {
removeViewLocked(index, true);
}
}
throw e;
}
}
//从 root.setView(view, wparams, panelParentView); 可以看出,真正添加View(Window)的其实就是ViewRootImpl了,而root.setView()方法中又调用了requestLayout()来完成异步刷新请求,requestLayout()又会调用performTraversals来完成View的绘制,又接着会通过IWindowSession的addToDisplay(mWindow, mSeq, mWindowAttributes, getHostVisibility(), mDisplay.getDisplayId(),mAttachInfo.mContentInsets, mInputChannel)方法来完成Window的添加过程,IWindowSession是一个Binder对象,真正的实现类是Session,也就是说这其实是一次IPC过程,远程调用了Session中的addToDisPlay方法。 下面是Session中的addToDisPlay方法:
@Override
public int addToDisplay(IWindow window, int seq, WindowManager.LayoutParams attrs,
int viewVisibility, int displayId, Rect outContentInsets,
InputChannel outInputChannel) {
return mService.addWindow(this, window, seq, attrs, viewVisibility, displayId,
outContentInsets, outInputChannel);
}
可以看出,Window的添加请求就交给WindowManagerService去处理了。addView大概一个过程如下:
WindowManager——>WindowManagerGobal——>ViewRootImpl——>Session——>WindowManagerService
在ActivityThread中,当Activity对象被创建完毕后,会将DecorView添加到Window中,同时会创建ViewRootImpl对象,并将ViewRootImpl对象和DecorView建立关联,可以参考一下代码,在ActvityThread中,也就是ViewRootImpl是DecorView的父元素,但是ViewRootImpl并不是View。
r.window = r.activity.getWindow();
View decor = r.window.getDecorView();
decor.setVisibility(View.INVISIBLE);
//getWindowManager这里用父类引用指向子类对象WindowManager
ViewManager wm = a.getWindowManager();
WindowManager.LayoutParams l = r.window.getAttributes();
a.mDecor = decor;
l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
l.softInputMode |= forwardBit;
if (a.mVisibleFromClient) {
a.mWindowAdded = true;
//WindowManager添加decorView
wm.addView(decor, l);
}