将view添加到window中:
activity.makeVisible方法: wm.addView(mDecor, getWindow().getAttributes());
该方法将DecorView添加到Window上,将view和window之间建立了联系;
Phone对象:
再attach方法中:新建一个PhoneWindow对象,同时得到一个WindowManager对象,WindowManager是一个抽象类,这个WindowManager的具体实现是在WindowManagerImpl;
每个Activity对象都会有一个WindowManager对象,这个mWindowManager就是和WindowManagerService进行通信,也是WMS识别View具体属于那个Activity的关键,创建IBinder类型的mToken;
首先再Activity的setContentView方法调用:
public void setContentView(@LayoutRes int layoutResID) {
getWindow().setContentView(layoutResID);
initWindowDecorActionBar();
}
该方法首先要获取Window对象,该对象的实例时PhoneWindow对象,是在创建Activity时,调用attach方法中创建的; 然后调用PhoneWindow对象的setContentView方法;
PhoneWindow类的setContentView方法:
public void setContentView(int layoutResID) {
if (mContentParent == null) {
installDecor();
} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
mContentParent.removeAllViews();
}
if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
getContext());
transitionTo(newScene);
} else {
mLayoutInflater.inflate(layoutResID, mContentParent);
}
mContentParent.requestApplyInsets();
final Callback cb = getCallback();
if (cb != null && !isDestroyed()) {
cb.onContentChanged();
}
mContentParentExplicitlySet = true;
}
该方法执行的步骤是:
1.判断mContentParent对象,是否为空,如果为空调用installDecor方法,来创建mContentParent和DecorView对象;
2.调用mLayoutInflater.inflate(layoutResID, mContentParent)方法,将xml解析成view树;
将view添加到Window上:
Activity再执行完onCreate时并不可见,只有在执行onResume之后Activity中的内容才是可见的;原因是onCreate阶段只是初始化类Activity需要显示的内容,而在onResume阶段才会将PhoneWindow中的DecorView真正的绘制到屏幕上;
首先分析HandleResumeActivity方法:
public void handleResumeActivity(IBinder token, boolean finalStateRequest, boolean isForward,
String reason) {
final Activity a = r.activity;
if (r.window == null && !a.mFinished && willBeVisible) {
r.window = r.activity.getWindow();
View decor = r.window.getDecorView();
decor.setVisibility(View.INVISIBLE);
ViewManager wm = a.getWindowManager();
WindowManager.LayoutParams l = r.window.getAttributes();
a.mDecor = decor;
l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
l.softInputMode |= forwardBit;
if (r.mPreserveWindow) {
a.mWindowAdded = true;
r.mPreserveWindow = false;
ViewRootImpl impl = decor.getViewRootImpl();
}
if (a.mVisibleFromClient) {
if (!a.mWindowAdded) {
a.mWindowAdded = true;
wm.addView(decor, l);
} else {
a.onWindowAttributesChanged(l);
}
}
}
代码中可以看出a.mDecor = decor将decor绑定到Activity上,而wm.addView(decor, l)将decor添加到屏幕上;
wm.addView方法分析:
WindowManager是接口类型,上文中我们了解到它真正的实现者是WindowManagerImpl类,方法如下:
public void addView(View view, ViewGroup.LayoutParams params,
Display display, Window parentWindow, int userId) {
ViewRootImpl root;
View panelParentView = null;
synchronized (mLock) {
root = new ViewRootImpl(view.getContext(), display);
view.setLayoutParams(wparams);
mViews.add(view);
mRoots.add(root);
mParams.add(wparams);
try {
root.setView(view, wparams, panelParentView, userId);
} catch (RuntimeException e) {
if (index >= 0) {
removeViewLocked(index, true);
}
throw e;
}
}
}
从上述方法可以看出,创建了ViewRootImpl类的对象root,然后调用setView方法将view添加到WMS中;
ViewRootImpl类的setView方法:
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView,
int userId) {
requestLayout();
res = mWindowSession.addToDisplayAsUser(mWindow, mSeq, mWindowAttributes,
getHostVisibility(), mDisplay.getDisplayId(), userId, mTmpFrame,
mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
mAttachInfo.mDisplayCutout, inputChannel,
mTempInsets, mTempControls);
}
再第一个方法requestLayout中,用于view的绘制,确保view再添加到Window上显示之前,完成测量和绘制的操作;
第二个方法调用了mWindowSession的addToDisplay方法,这个方法将View添加到WMS上; 其中mWindowSession对象是WindowManagerService中的Session对象;