一、主要功能
(1)计算与管理窗口的位置、层次(控制窗口的显示与隐藏的以及要显示的位置)
(2)根据(1)来渲染窗口动画
(3)持有SurfaceControl,在Surface初始化的时候,会初始化一个Canvas(画布)
二、主要成员
mDecor:DecorView的实例,标示Window内部的顶级视图。
mContentRoot:是DecorView的唯一子视图,内部包含mContentParent、标题栏、状态栏,在Android源码中,有9种不同的布局来对应不同主题。
mContentParent:Activity所调用的setContentView所设置的布局文件就会加到这个视图中。
获取
mContentRoot -> getWindow().getDecorView();
mContentParent -> ((ViewGroup)context.findViewById(android.R.id.content)).getChildAt(0);
mContentParent -> getWindow().getContentView();
三、Activity 、Window、View
Activity是系统可视化交互组件,四大组件都由AMS统一管理生命周期,事实上它的职责只是生命周期的管理,由设计模式的单一职责的原则,那势必需要将Activity和其上的视图View进行解耦,那么就引入Window的概念,它是个抽象类,对于Activity来说,它的具体实现类是PhoneWindow,在Activity执行attach的时候,会创建了一个PhoneWindow对象。PhoneWindow作为装载根视图DecorView的顶级容器,Activity通过setContentView实际上是调用PhoneWindow来创建DecorView,并解析xml布局加载到DecorView的contentView部分。
四、WindowManager、WindowManagerImpl、WindowManagerGlobal
WindowManager是一个接口类,继承自接口ViewManager,负责窗口的管理(增、删、改)。它的实现类是WindowManagerImpl,而具体操作实际上又会交给WindowManagerGlobal来处理,它是个单例,进程唯一。WindowManagerGlobal执行addView的方法中会传入DecorView, 还会初始化一个ViewRootImpl。WindowManagerGlobal因为是单例的,它内部会有两个List来分别保存这两个对象,来统一管理。
参考:
https://www.jianshu.com/p/3b5b6f2469d8
https://blog.csdn.net/itachi85/article/details/78186741?utm_source=blogxgwz9