转载请注明出处:http://blog.csdn.net/crazy1235/article/details/72625679
关于Window对象管理的相关类:
ViewManager
WindowManager
WindowManagerImpl
WindowManagerGlobal
ViewParent
ViewRootImpl
ActvityThread
WindowManagerService
Window是一个抽象类,具体实现类就是 PhoneWindow !
一个Window是通过一个View来承载的,还对应一个ViewRootImpl!
访问Window必须通过WindowManager来实现!
Window具体操作方法实现实在WindowManagerImpl类中,方法定义在ViewManager接口中!
public interface ViewManager
{
public void addView(View view, ViewGroup.LayoutParams params);
public void updateViewLayout(View view, ViewGroup.LayoutParams params);
public void removeView(View view);
}
ViewManager接口定义了三个函数,分别对应window的添加,更新和删除!
addView
@Override
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
applyDefaultToken(params);
mGlobal.addView(view, params, mContext.getDisplay(), mParentWindow);
其实WindowManagerImpl也没有做具体的操作,而是交给了 WindowManagerGlobal
在WindowManagerImpl类中进行了初始化
private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();
WindowManagerGlobal.java
private WindowManagerGlobal() {
}
public static WindowManagerGlobal getInstance() {
synchronized (WindowManagerGlobal.class) {
if (sDefaultWindowManager == null) {
sDefaultWindowManager = new WindowManagerGlobal();
}
return sDefaultWindowManager;
}
}
很明显看出这是个单例模式!
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>();
WindowManagerGlobal中有四个重要的集合!
mViews集合存储了所有window对应的View对象!
mRoots集合存储了所有window对应的ViewRootImpl对象
mParams集合存储了所有window对应的布局参数
mDyingViews集合存储了所有正在被删除的window对象
public void addView(View view, ViewGroup.LayoutParams params,
Display display, Window parentWindow) {
// 1. 验证参数!
if (view == null) {
throw new IllegalArgumentException("