1. Read Android Kernel Analysis.
2. Basic concepts of the UI framework:
Activity: A Basic page unit. Activity contains a window, which can be used to draw various views.
View: the most basic UI component, indicating a rectangular area on the screen;
Window: indicates the top-level window, which manages the display of the interface and Event Response. Each activity creates
Phonewindow object, which is the interface for interaction between activity and the entire view system
Phonewindow class: This class inherits from the window class, And the phonewindow class contains a decorview object. In short, phonewindow encapsulates a framelayout and provides a set of common window operation interfaces.
Decorview: it is the rootview of the view in the window and sets window properties. This class is a subclass of framelayout and an internal class in phonewindow. Decorview indicates decoration. decorview modifies a common framelayout, for example, adding a general title.
Bar, and response to specific key messages.
Viewroot: it is not a view type, but a handler.
Its main functions are as follows:
A. Distribute events initiated by users to decorview, such as buttons, touch screens, and trackball events;
B. interact with windowmanagerservice to draw the GUI of the entire activity
3.
The drawing process of the entire view tree is expanded in the javasmtraversals () function of the viewroot. Java class. The execution process of this function is as follows:
Determine whether to recalculate the View Size (measure), whether to relocate the view location (layout), and whether to re-paint the view based on the previously set status.
(Draw), the Framework process is as follows:
The step is host. layout ()
Next, let's take a look at the structure of the entire view tree. The operation on each specific view object is actually a recursive implementation.
4. mesarue ():
Calculate the actual size for the entire view tree, that is, set the actual height (corresponding attribute:Mmeasuredheight) And width (corresponding property:
Mmeasurewidth), The actual width and height of each view control are determined by the parent view and its own view.
Layout:
Place the view tree to a proper position based on the size of the sub-view and the layout parameters.
Draw ():
Call the draw () method to draw the view tree. It is worth noting that
The view of each view tree is re-drawn, and only the views that need to be re-painted are re-drawn. The internal variables of the View class contain a flag drawn.
When a view needs to be re-painted, the flag is added to the view.
5. invalidate () method
:
Request to re-paint the view tree, that is, the draw () process. If the View Size does not change, the layout () process will not be called, and only the "to be re-painted"
View, that is, if you (view, only draw the view; viewgroup, draw the entire viewgroup) Request invalidate () method, to draw the view.
6.
Requestlayout () method: The measure () process and layout () process are called.
Note: The layout relayout process for the view tree includes the measure () and layout () processes. The draw () process is not called, but is not re-drawn.
Any view includes the caller itself.
7. Measure Process:
The view tree is drawn through traversal. The internal main logic is to determine whether to re-measure the View Size (measure), whether to re-layout (layout), and whether to re-draw (draw ). The measure process is a prerequisite for traversal. layout and draw can be performed only after measure ), because the measurement size of each view calculated during the measure process needs to be used in the layout process, and the draw process requires layout to determine the position of each view before drawing. Next we will mainly discuss the main process of measure, which is difficult to understand compared with layout, draw, and measure.
When compiling the layout XML file, we will encounter the layout_width and layout_height attributes. For these two attributes, we have three options: assign a value to a specific value, match_parent or wrap_content, the measure process is used to process match_parent (fill_parent in earlier versions) or wrap_content. If layout specifies that the layout_width and layout_height values of all views must be assigned to specific values, therefore, measure is not necessary, but there must be a reason for Google to consider adding match_parent or wrap_content to Android design. They will make the layout more flexible.
Take a look at the two parameters in measue (INT widthmeasurespec, int heightmeasurespec). These two parameters are the measurement specifications provided by the parent view, respectively, when the parent view calls the measure function of the Child view to measure the child view, these two parameters are passed in. The two parameters and the layoutparams of the child view are used to jointly determine the measurement specifications of the Child view, this process is embodied in the measurechildwithmargins function of viewgroup, which will be introduced later.
The value of the measurespec parameter is int type, which can be divided into high 32-bit and low 16. The high 32-bit stores specmode, and the low 16-bit represents specsize. specmode is divided into three types:
1. measurespec. unspecified (INT value: 0). The parent view does not impose any restrictions on the Child view. The child view can obtain any desired size;
2. measurespec. Exactly. The size of the parent view is the size specified in specsize;
3. measurespec. at_most. The size of the sub-view is the maximum size in specsize.
When the parent view has no limit on the Child view, it generally uses measue (), that is, specmodemeasurespec. unspecified
Measurespec has three modes: unspecified, exactly, and at_most. What is the relationship between these modes and the layout parameter fill_parent and wrap_content we usually set. After code testing, we know that when we set the width or height to fill_parent, the container calls the measure method of the sub-view during layout and the input mode is exactly, because the sub-view occupies the space of the remaining container, its size is determined. When wrap_content is set, the container transmits at_most,
Indicates the maximum size of a sub-view, so that the view will set its own size based on the upper limit. When the size of the sub-view is set to an exact value, the container imports exactly
8. fill_parent should be a sub-view that occupies the space of the remaining container, instead of overwriting other view spaces that have been previously laid out. Of course, there is no space allocated for the sub-view layout in the back, therefore, the fill_parent attribute is very important to the layout order. What I previously thought was to fill up all the container space. No wonder Google changed the fill_parent name to match_parent in version 2.2.