RootViewImpl

ViewRoot,或者说它的实现类ViewRootImpl,跟View没有任何关系。它不是View的子类或父类。对于结构而言,在大部分正常情况下,一棵ViewTree的根节点往往是DecorView,而DecorView的根则是PhoneWindow,跟ViewRoot真没什么关系。

Activity视图层结构

activity视图结构
activity视图结构2

Activity的UI架构图(图是网上找的)。从图中可以清楚的看出,Activity的UI结构。其实每个Activity中都包含一个Window对象,通常,Android中的Window是由PhoneWindow实现的。而PhoneWindow又将一个DecorView设置为整个窗口的根View(DecorView是一个ViewGroup)。

ViewRootImpl简介

The top of a view hierarchy, implementing the needed protocol between View and the WindowManager. This is for the most part an internal implementation detail of {@link WindowManagerGlobal}.

public final class ViewRootImpl implements ViewParent,
        View.AttachInfo.Callbacks, HardwareRenderer.HardwareDrawCallbacks {
    ...
}

ViewRootImpl是View中的最高层级,属于所有View的根(但ViewRootImpl不是View,只是实现了ViewParent接口),实现了View和WindowManager之间的通信协议,实现的具体细节在WindowManagerGlobal这个类当中。

我们知道Activity中有Window对象,一个Window对象对应着一个View(DecorView),ViewRootImpl就是对这个View进行操作的。

我们知道界面所有的元素都是有View构成的,界面上的每一个像素点也都是由View绘制的。Window只是一个抽象的概念,把界面抽象为一个窗口对象,也可以抽象为一个View。

ViewRootImpl与其他类之间的关系

image.png
public final class WindowManagerGlobal {
    /*******部分代码省略**********/
    //所有Window对象中的View
    private final ArrayList<View> mViews = new ArrayList<View>();
    //所有Window对象中的View所对应的ViewRootImpl
    private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();
    //所有Window对象中的View所对应的布局参数
    private final ArrayList<WindowManager.LayoutParams> mParams = new ArrayList<WindowManager.LayoutParams>();
}

WindowManagerGlobal在其内部存储着ViewRootImplView实例的映射关系(顺序存储)。

WindowManager继承自ViewManger,从ViewManager这个类名来看就是用来对View类进行管理的,从ViewManager接口中的添加、更新、删除View的方法也可以看出来WindowManagerView的管理。

WindowManagerImplWindowManager的实现类。WindowManagerImpl内部方法实现都是由代理类WindowManagerGlobal完成,而WindowManagerGlobal是一个单例,也就是一个进程中只有一个WindowManagerGlobal对象服务于所有页面的View。

ViewRootImpl的初始化

在Activity的onResume之后,当前Activity的Window对象中的View会被添加在WindowManager中。

Activity添加过程.png

创建Window所对应的ViewRootImpl,并将Window所对应的View、ViewRootImpl、LayoutParams顺序添加在WindowManager中。

下图黄色部分创建了ViewRootImpl。

系统窗口添加过程.png

ViewRootImpl绑定Window所对应的View

ViewRootImpl的setView(View view, WindowManager.LayoutParams attrs, View panelParentView)方法绑定Window所对应的View,并对该View进行测量、布局、绘制等。

对View的操作包测量、布局、绘制,其过程主要是在ViewRootImpl的performTraversals方法中。

View的测量

ViewRootImpl调用performMeasure执行Window对应的View的测量。

  • ViewRootImpl的performMeasure;
  • DecorView(FrameLayout)的measure;
  • DecorView(FrameLayout)的onMeasure;
  • DecorView(FrameLayout)所有子View的measure;

View的布局

ViewRootImpl调用performLayout执行Window对应的View的布局。

  • ViewRootImpl的performLayout;
  • DecorView(FrameLayout)的layout方法;
  • DecorView(FrameLayout)的onLayout方法;
  • DecorView(FrameLayout)的layoutChildren方法;
  • DecorView(FrameLayout)的所有子View的Layout;

在这期间可能View会自己触发布布局请求,所以在此过程会在此调用ViewRootImpl的requestLayout重新进行测量、布局、绘制。

View的绘制

ViewRootImpl调用performDraw执行Window对应的View的布局。

  • ViewRootImpl的performDraw;
  • ViewRootImpl的draw;
  • ViewRootImpl的drawSoftware;
  • DecorView(FrameLayout)的draw方法;
  • DecorView(FrameLayout)的dispatchDraw方法;
  • DecorView(FrameLayout)的drawChild方法;
  • DecorView(FrameLayout)的所有子View的draw方法;

因为Android中Window的显示要从根view(DecorView)开始画起,所以额外定义了一个持有根View的RootView,在这个类里面开始出发整个视图的测量、布局、绘制等操作

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容