android view works (a)

Preface:

On the Android interface provides a set of GUI libraries, there are a lot of controls, but many times we are not satisfied with the control system of the system, causing the same kind of application interface more serious, so we need to customize the view, you can achieve different effects, in order to better customize the view. also you need to grasp the underlying principle of View. For example: the measurement view of the process. The layout process and rendering process, so that we can define their desired effect

There are several custom view of a fixed type. Some directly inherited from view and viewGroup. Some choose to inherit the existing system of controls,

 

1. viewRoot and DecorView

viewRoot corresponds viewRootImpl class, and it is the link windowManager decorView when Activity object is created, will be added to the window DecorViewt and create viewRootImpl objects,

And viewRootImpl and decorView establish a connection;

View drawing process is to start from preformTraversals viewRoot method, after the measure, layout, draw a view three processes will eventually drawn.

Roughly about painting a flow chart (Figure words):

                 The flowchart preformTraversals

According to the description on the map: performTraversals will first call performMesasure, performLayout, and performDrawa, these three methods to complete the top view of mesaure, layout, draw process, which calls back to the measure in performMeasure method, the method will be called measure onMeasure method, the method will be carried out in onMeasure all child elements measure the process, this time to measure the flow passed from parent to child elements in the container, to complete a measure process, followed by repeated measure subelements parent container, complete view the entire tree traversal. Similarly, transfer processes and perfromDraw perfromLayout are similar, the only difference is achieved by perfromDraw dispatchDraw transfer process in the draw method,

Measure process determines the width and height of the view, the measure is completed, and can be acquired by getMeasuredWidth getMesauredHeight width and height of the view,

Draw process determines View the display only after the completion of the draw method to render the content on the screen.

2) DecorView (top view), the interior contains LinearLayout a vertical direction, in this linearLayout has upper and lower parts, the top is the title bar, below the content column, at the same time, through the source code we can know, DecorView actually a frameLayout , view events go through DecorView, then passed to our View; (drawing clearer)

           Top View: The structure DecorView

 

2. Customize view of the important things (MeasureSpec)

1) MeasureSpec largely determines View of sizes. This process is affected parent container, during the measurement, the system will view the layoutParmas is converted according to the specification of the parent container MeasureSpec applied, and then measured for width and height based on this view MesureSpec.

What 2) MesaureSpec that?

MeasureSpec 代表的是一个32位的int值,高两位代表SpecMode(测量模式),低20位代表SpecSize(测量大小);

MeasureSpec通过将SpecMode和SpecSize打包成一个int值,避免过对的对象内存分配。一组SpecMode和specSize可以打包为一个MeausreSpec,也可以将MeasureSpec进行解包的形式得到原始的SpecMode和SpecSize,

3)SpecMode的三个类,

1.UNSPECIFIED

父容器不对view有任何限制,要多大有多大,常用于系统内部,表示一种测量状态

2.EXACTLY

父容器已经检测出View的所需要的精确大小,这个时候View的最终大小就是SpecSize所指定的值,(match_parent)

3.AT_MOST

父容器指定一个可用的大小specSize. View的大小不能大于这个值,(wrap_content)

 

3.MeasureSpec和LayoutParams的对应关系

正常情况下我们使用view指定的MesaureSpec来对view进行测量,但是我们可以给View设置LayoutParams,在view测量的时候,系统会将layoutParams在父容器的约束下转换为对应的MeausreSpec。

对于DecorView,其MeasureSpec由父容器的MeasureSpec和自身的LayoutParams来共同决定,

对于普通view,其MeasureSpec由父容器的MeasureSpec和自身的layoutParams来共同决定的。

通过如下代码展示DecorView的MeasureSpec的创建过程:

getRootMeasureSpec的方法实现:

通过上述代码:DecorView的MeasureSpec的产生过程很明确了,具体来说遵守如下规则,根据LayoutParams中的宽高参数划分:

  1. LayoutParams.MATCH_PARENT:精确模式,大小就是窗口的大小。
  2. LayoutParams.WRAP_CONTENT : 最大模式,大小不定,但是不能超过窗口的大小;
  3. 固定大小(比如100dp):精确模式,大小为layoutParams中指定的大小;
  4. 通过表格的形式展现view的MeasureSpec的创建规则:

发布了96 篇原创文章 · 获赞 370 · 访问量 42万+

Guess you like

Origin blog.csdn.net/wk_beicai/article/details/89670669
Recommended