1. 说明
今天我们来看下Window底层原理,这个就要求我们必须对下边的知识要非常之清楚:
当app一运行到手机上边时,我们一定要清楚的知道:
一定要在心里面对FrameWork层的源码有一个架构、Activity是怎么启动的、布局文件是怎样加载的、资源怎么加载的、View的绘制流程、View的Touch事件、IPC进程间通信机制、Handler源码分析
2. 分析
我们一开始开发时可能听的最多的就是四大组件,那么到后期可能就会听到AMS以及其他的一些名词,那么我们就来看下这些都代表什么意思?
AMS:指的是 ActivityManagerService 有Activity、ActivityManager;
WMS(窗口有关):Window、WindowManager、WindowManagerService、Token、Session、ViewRootImpl、View(我们就只是讲了DecorView)
当面试如果问你AMS和WMS是怎样建立联系的,也就是说Activity、View、Window之间的联系是怎样的?
回答:
其实ActivityManagerService和WindowManagerService的设计模式以及它们的设计风格其实是差不太多的,都是跨进程之间的通讯的,AMS有Activity、ActivityManager、ActivityManagerService,而WMS中有Window、WindowManager、WindowManagerService,只不过WMS中有Token、Session、ViewRootImpl(DecorView是根布局)
Activity中有PhoneWindow,PhoneWindow中有一个根布局DecorView,会首先给WindowManager,然后WindowManager其实什么都没做,它是交给WindowManagerGlobal这个单例来统一去管理,因为只有单例才适合去做我们的管理类,原因就是 单例是一直存在于我们的内存中,不会随着Activity的退出而销毁。而DecorView传递到WindowManagerGlobal之后,它会去创建ViewRootImpl ,即就是ViewRoot的实现类,ViewRootImpl这个实现类有2个作用:
第一个就是和远程的WindowManagerService进行通信的,会首先从远程的服务端中去获取session来通信,因为我们应用无法操作系统的布局,比如说点击输入法,就需要通知系统让输入法弹起来,所以只能通过这样的方式来进行通信;
第二个就是与View的绘制流程 ;
那么我们接下来就来看下app刚运行到手机上边时,我们都做了哪些工作?
2.1 当手机刚一运行app时候调用setContentView() ->
2.2 Activity里边有PhoneWindow ,调用PhoneWindow设置setContentView()布局 ,而setContentView()方法中就是去创建一个根View,即就是DecorView ,然后加载系统默认的布局,然后解析我们设置的资源布局添加到android.R.id.content中,也就是添加到ContentParent中。PhoneWindow的setContentView()这个方法执行完毕后只是去解析加载我们的布局,即就是activity_main.xml文件,然后什么都没干->
那么我们自己的activity_main.xml的布局文件是怎样显示出来的呢???
3. 布局的显示绘制流程
Activity中的源码,里边有一个PhoneWindow:
final void attach(Context context, ActivityThread aThread,
mWindow = new PhoneWindow(this, window);
PhoneWindow中的源码,里边有一个setContentView()方法:
@Override
public void setContentView(int layoutResID) {
// Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
// decor, when theme attributes and the like are crystalized. Do not check the feature
// before this happens.
if (mContentParent == null) {
installDecor();
} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
mContentParent.removeAllViews();
}
3.1 activity_main.xml布局文件是怎样显示出来的?
Activity中的attach()方法,会绑定设置Activity的一些参数,Activity里边有一个PhoneWindow,即就是new PhoneWindow() ,Window里面有一个WindowManager
final void attach(Context context, ActivityThread aThread,
Instrumentation instr, IBinder token, int ident,
Application application, Intent intent, ActivityInfo info,
CharSequence title, Activity parent, String id,
NonConfigurationInstances lastNonConfigurationInstances,
Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Window window) {
attachBaseContext(context);
mFragments.attachHost(null /*parent*/);
mWindow = new PhoneWindow(this, window);
mWindow.setWindowControllerCallback(this);
mWindow.setWindowManager(
(WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
mToken, mComponent.flattenToString(),
(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
if (mParent != null) {
mWindow.setContainer(mParent.getWindow());
}
mWindowManager = mWindow.getWindowManager();
mCurrentConfig = config;
}
Activity是通过反射创建出来的,在ActivityThread中的performLaunchActivity()方法中
WindowManager.LayoutParams 和RelativeLayout.LayoutParams差不多,而RelativeLayout.LayoutParams是用来描述布局的一些信息的,意思就是:
比如在activity_main.xml布局文件中有一个TextView,如果你的根布局是RelativeLayout,并且给TextView设置了
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
那么对于TextView是有作用的;
如果你的根布局是LinearLayout的话,那么设置这两个属性就没有作用,因为LinearLayout源码中就没有定义这两个属性,就像RelativeLayout源码中没有定义
android:layout_gravity="center"
android:gravity="center"
这两个属性一样。
3.2 wm.addView(decor , 1);
这个方法在布局的绘制流程中看过,WindowManager的实现类是WindowManagerImpl,实际上调用的是WindowManagerGlobal,而WindowManagerGlobal是一个单例,单例设计模式意思就是一直在我们的内存中,不会随着Activity的退出而销毁,而我们项目中肯定有很多的Activity,也会对应的有很多的布局文件,所以它就会去管理Activity,而且像一些管理类都是单例
findViewLocked(view , false);意思就是如图所示:
Activity1、Activity2都会去调用setContentView()方法,而且他们都会有一个DecorView,而这两个Activity都是由 WindowManagerGlobal这个单例来管理
ViewRootImpl
总结布局文件绘制流程顺序如下
activity ->
PhoneWindow(DecorView) ->
WindowManager(不是单例,所以有多个) ->
WindowManagerGlobal(是单例,只有1个) ->
ViewRootImpl(有多个,但是一个ViewRootImpl只会绑定一个DecorView)
也就是说:
activity里边有一个PhoneWindow ->
PhoneWindow里边有一个DecorView ,DecorView最终是交给一个全局变量去管理的,而这个全局变量中就存储了很多的变量,有ViewRootImpl ->
而ViewRootImpl是用来和远程服务去通信的 ->
页面是交给WindowManagerGlobal这个单例类去管理的
跨进程通信就意味着:app应用和系统服务一定要建立连接
4. requestLayout()方法 —— 百度、阿里、腾讯等等大公司都会必问的一个问题
是View绘制流程的入口,在这个方法中做了3件事
performMeasure()、performLayout()、performDraw(),测量、摆放、绘制
这个很重要,百度、阿里、腾讯等等大公司都会必问的一个问题,就是View的绘制流程