UI rendering process, so many Android engineer can not start? Article will teach you to read!

Foreword

For the UI system which we often encounter in the android them when drawing the UI of a variety of problems and not know where to solve, sometimes when you change the need to develop custom components, need to make their own adjustments, or achieve a custom idea when special effects are not clear, you want to achieve the most basic part of the UI Fun, it is to draw a comprehensive insight into the process of the UI. so then take you to a comprehensive analysis of the overall UI rendering system.

Ideas: android program starts - → Activity loaded and complete life cycle - → setContentView- → graphics rendering

doubt:

How 1.Android program is to start, how to call Activity life cycle?

2. Our setContentView how to load UI files in the Activity onCreate them?

3.UI is how to draw?

answer:

1.Android program flow

As we all know, we want to open java program need to rely on the main method, which is our program entry (the main thread) to enter, but in which we develop android routine procedure for we have not found the main method, then the android among how to start running?

Familiar friends may know that there is a named ActivityThread class in android among this class represents the main thread android them, and in this class which we see the more familiar main method, does it now believe that our android when you open the app is the first call of this class is the main current, which is here as our starting point

UI rendering process, so many Android engineer can not start? Article will teach you to read!

Here you can see a call Activity attach () method

UI rendering process, so many Android engineer can not start? Article will teach you to read!

Here we might first consider what is getService out?

Once inside, we will find

UI rendering process, so many Android engineer can not start? Article will teach you to read!

Among this, which is called the ActivityManagerService the service system, and gives a Binder Interface

Here, then, we can think of, binder communication mechanism among the android, then in fact we have ActivityManager system service call management, and by calling the binder among the interface, which is why we are talking about inter-process access Activity s reason

UI rendering process, so many Android engineer can not start? Article will teach you to read!

那么明白了这个时候能够得到ActivityManager之后,我们接着回到attach当中继续看下去, 这个时候会发现,我们调用了一个attachApplication方法(见图2)这个方法又是干嘛的?attachApplication在这里的作用其实实际上是ActivityThread通过attach获取到,然后将applciationThread将其关联,把activity相关信息存储在applciationThread里面,apllicationThread的类为activity的各种状态做了相对应的准备工作

UI rendering process, so many Android engineer can not start? Article will teach you to read!

这个时候我们需要关注,ApplicationThread当中做了什么?

当我们打开ApplicationThread中我们会看到一堆的schedle方法,这些方法的名称其实就可以给我们表明,代表的是在执行Activity的某种状态时调用的计划执行方法

这时我们会看到一个scheduleLaunchActivity方法,表示计划加载时调用的

这里我发现了一个很有意思的事情

UI rendering process, so many Android engineer can not start? Article will teach you to read!

UI rendering process, so many Android engineer can not start? Article will teach you to read!

这个上面我们会看到一个ActivityClientRecord对象,这个对象其实实际上就是我们的Activity

而且似乎每一个方法还干了一件让我们非常熟悉的一件事, 进行了一次sendMessage()将当前创建的Activity发送了出去

UI rendering process, so many Android engineer can not start? Article will teach you to read!

当走到这里我们会发现最终我们调用的是Handler的消息通信机制,也就是说,在这里我们可以总结一下,

当Activity状态改变时,都会有对应的一个消息发送出去

而接收这里,我能发现通过发送时不同的状态,这边调用了不同的handlerXXXActivity方法

UI rendering process, so many Android engineer can not start? Article will teach you to read!

在这里,我门貌似发现了Activity的生命周期的调用痕迹,那么其实到此为止,我门可以得出一个结论,

Application运行的过程当中,对于Activity的操作,状态转变,其实实际上是通过Handler消息机制来完成的,

Application当中只管去发, 由消息机制负责调用,因为在main方法当中我门的Looper轮训器是一直在进行轮训的

而当我们在加载Activity的时候,当中调用了一个performLaunchActivity()方法,在这个中间我发现了我们onCreate的调用痕迹

UI rendering process, so many Android engineer can not start? Article will teach you to read!

也就是说,到目前为止我们能够明白,整个Application加载Activity的整套流程是怎么回事

那么接下来我们需要关注的是,在onCreate当中我们所写的setContentView到底干了什么

2.setContentView

在onCreate当中我们往往会使用setContentView去进行设置我们自己的布局文件或者view,那么在这当中他到底是怎么做的?通过观察源码,这个时候通过一系列线索我找到了最终的位置PhoneWindow类

UI rendering process, so many Android engineer can not start? Article will teach you to read!

这个时候我们会看到他做了两个事情,一个是installDecor,另一个是inflate,这两个后一个不难猜出他是在进行布局文件的解析, 前面的我们认为她是在初始化某个东西

UI rendering process, so many Android engineer can not start? Article will teach you to read!

进来之后发现他初始化了两个东西,一个叫做mDecor,一个叫做mContentParent

UI rendering process, so many Android engineer can not start? Article will teach you to read!

UI rendering process, so many Android engineer can not start? Article will teach you to read!

我们看到了mDecor是一个DecorView

mContentParent是一个ViewGroup

透过注释的翻译,其实我们就能很明确知道这两个是用来干嘛的

// This is the view in which the window contents are placed. It is either(这是放置窗口内容的视图)

// mDecor itself, or a child of mDecor where the contents go.(它要么是mDecor本身,要么是mDecor的子类的内容。)

//This is the top-level view of the window, containing the window decor.(这是在窗口当中的顶层View,包含窗口的decor)

一个代表的是顶层view,一个用来装他下面的视图内容

在接着往下看的时候,我门发现,generateLayout方法当中,发现了在此处进行了大量的requestFeature的调用,也就是所,我们的requestFeature

UI rendering process, so many Android engineer can not start? Article will teach you to read!

然后在下面我门会发现在做了一件事情,

UI rendering process, so many Android engineer can not start? Article will teach you to read!

UI rendering process, so many Android engineer can not start? Article will teach you to read!

UI rendering process, so many Android engineer can not start? Article will teach you to read!

当前这里竟然在加载布局文件,并且生成了一个view, 但是好像貌似不是我门自己的

所以我们需要去探寻他到底加载了一个什么东东?

UI rendering process, so many Android engineer can not start? Article will teach you to read!

这是我找到了一个比较有意思的组件,

在这个上面我看到了一句这样的注释

//This is an optimized layout for a screen, with the minimum set of features

enabled.

这是一个屏幕的优化布局,具有最小的特征集启用。

通过注释和一些资料分析, 得到了一个比较坑的结果。

UI rendering process, so many Android engineer can not start? Article will teach you to read!

这是DecorView默认的一个渲染,然后我门自己的布局都是渲染到她的FrameLayout上的

那么在这里我门现在能够明白,installDector其实实际上是在初始化两个视图容器,然后加载系统的R资源及特征,产生了一个基本布局

那么接着回到之前我门关注的另外一个方法mLayoutInflater.inflate(layoutResID, mContentParent);

这个方法就比较好理解了,

UI rendering process, so many Android engineer can not start? Article will teach you to read!

这这段注释上面我门就可以得到一个信息

//Inflate a new view hierarchy from the specified xml resource.(从指定的视图当中获取试图的层次结构,意思就是,现在在加载自己的资源)

而具体流程就不贴代码了给各位上一张图

UI rendering process, so many Android engineer can not start? Article will teach you to read!

那么在这里我门就能够明白,setContentView其实做了两件比较核心的事情,就是加载环境配置,和自己的布局,那么接下来我门需要考虑的事情就是,他到底怎么画到界面上的

3.UI是如何绘制的?

通过前面两个章节,我门了解到,程序对于activity生命周期的调用,以及我们的视图资源的由来。这是我门需要找到的是我门的绘制起点在哪?

UI rendering process, so many Android engineer can not start? Article will teach you to read!

在ActivityThread启动时, 我发现在加载handleLaunchActivity方法调用performLaunchActivity方法之后又调用了一个handleResumeActivity在这里我发现了绘制流程的开始

UI rendering process, so many Android engineer can not start? Article will teach you to read!

通过前面的流程我门知道,onCreate之行完成之后,所有资源交给WindowManager保管

在这里,将我们的VIew交给了WindowManager,此处调用了addView

UI rendering process, so many Android engineer can not start? Article will teach you to read!

UI rendering process, so many Android engineer can not start? Article will teach you to read!

UI rendering process, so many Android engineer can not start? Article will teach you to read!

进入addView之后我们发现了一段这样的代码,他将视图,和参数还有我门的一个ViewRoot对象都用了容器去装在了起来,那么在此处我门可以得出,是将所有的相关对象保存起来

mViews保存的是View对象,DecorView

mRoots保存和顶层View关联的ViewRootImpl对象

mParams保存的是创建顶层View的layout参数。

而WindowManagerGlobal类也负责和WMS通信

而在此时,有一句关键代码root.setView,这里是将我们的参数,和视图同时交给了ViewRoot,那么这个时候我们来看下ViewRoot当中的setView干了什么

终于在这里让我发现了让我明白的一步

UI rendering process, so many Android engineer can not start? Article will teach you to read!

在这里我门会看到view.assignParent的设置是this, 那么也就是说在view当中parent其实实际上是ViewRoot

那么在setContentView当中调用了一个setLayoutParams()是调用的ViewRoot的

而在ViewRoot当中发现了setLayoutParams和preformLayout对requestLayout方法的调用

In requestLayout them a call to find scheduleTraversals method and scheduleTraversals which called access doTraversal eventually have access to the performTraversals (), and in this, and I found the whole process of calling draw

The current which in turn is used

UI rendering process, so many Android engineer can not start? Article will teach you to read!

UI rendering process, so many Android engineer can not start? Article will teach you to read!

UI rendering process, so many Android engineer can not start? Article will teach you to read!

UI layout drawing go back to measure, and then carrying out the layout of the place, when all of the layout of the place is completed to measure, draw.

At this point the whole UI rendering process we have been very clear.

We can operate your own custom components in accordance with the process of this draw.

BATJ, byte beating interview topics, topics algorithms, high-end technology topics, mixed-use development themes, java interview topics, Android, Java little knowledge, to performance optimization. .View.OpenCV.NDK thread, etc. have been uploaded to my GitHub

Click on my GitHub address:https://github.com/Meng997998/AndroidJX learn together under the star point

Guess you like

Origin blog.51cto.com/14606040/2465682