Detailed description of view drawing process in Android

Create window

Window is the window. This concept is implemented in the Android framework as the abstract class android.view.window, which abstracts the windows in the Android system. Before introducing this class, let's take a look at what is a window?

In fact, window is a macro idea. It is a rectangular area on the screen used to draw various UI elements and respond to user input events. It usually has the following two characteristics:

Draw independently without interaction with other interfaces;

Input events of other interfaces will not be triggered;

In Android system, the window is the display area of an exclusive surface instance, and the surface of each window is allocated by windowmanagerservice. We can think of the surface as a canvas on which applications can draw through canvas or OpenGL. After painting, multiple surfaces are mixed in a specific order (i.e. z-order) through surfaceflinger, and then output to framebuffer, so that the user interface can be displayed.

The abstract class android.view.window can be regarded as the Convention on the macro concept of window in Android, and the class phonewindow is the concrete implementation of the Android window concept provided by the framework. Next, let's introduce the abstract class Android. View. Window.

This abstract class contains three core components:

Windowmanager.layoutparams: layout parameters of the window;

Callback: callback interface of window, usually implemented by activity;

Viewtree: the control tree that the window hosts.

In the attach method of activity, create a window by calling policymanager.makenewwindo. When adding a view to WindowManager, windowmanagerimpl creates a viewroot to manage the root view of the window. And pass the view to viewroot through the viewroot.setview method.

Create decorview

Decorview is the topmost view of the entire window interface. The window object in the activity helps us create a top-level view of the phonewindow internal class decorview (the parent class is FrameLayout). Then, the XML content layout is parsed into a view tree structure through the layouteinflator and added to the FrameLayout parent container with ID content in the decorview top-level view. The content layout of the activity will eventually be added to the top-level view of the decorview window.

Create a viewroot and associate it with a view

Windowmanagerimpl saves the decorview to mviews and creates the corresponding viewroot; Viewroot is used to manage the root view of the window and interact with the global window manger. There is a nested class: W in viewroot. W is a binder subclass, which is used to receive various messages of global window manager, such as key messages, touch messages, etc. Viewroot has a W-type member mwindow. Viewroot creates an instance of W in the constructor and assigns it to mwindow. Viewroot is a subclass of handler. W will pass the message to viewroot through looper. Viewroot passes mwindow to windowsession in setview method.

Viewroot is the bridge between GUI management system and GUI presentation system. It should be noted that it is not a view type,. Its main functions are as follows:

1. Distribute the received event events initiated by the user to decorview, such as key press, touch screen, trackball and other events; 2. Interact with windowmanagerservice to complete the drawing of the GUI of the whole activity. View drawing basic process

Here is the drawing process of view in Android system: execute the following three methods in view class in turn:

Measure (int, int): measure the size of the view layout (int, int, int): set the position of the child view draw (canvas): draw the view content onto the canvas

The drawing process of the whole view tree is expanded in the performtraversals() function of the viewroot.java class. The execution process of this function can be simply summarized as judging whether the view size needs to be recalculated, whether the layout of the view needs to be relocated, and whether the measurement process needs to be redrawn

Main function: calculate the actual size of the entire view tree, that is, set the actual height (mmeasuredheight) and width (mmeasurewidth). The actual width and height of each view control are determined by the parent view and its own view.

The specific calls are as follows:

In the performTraversals method of ViewRootImpl, call measureHierarchy and call performMeasure.

The viewroot root object's attribute Mview (its type is generally ViewGroup type) calls the measure () method to calculate the size of the view tree, and the callback

The onmeasure() method of the view / ViewGroup object implements the following functions:

1. Set the final size of the view. This function is implemented by calling the setmeasureddimension () method to set the actual height (mmeasuredheight) and width (mmeasurewidth)

2. If the view object is a ViewGroup type, you need to override the onmeasure () method to traverse its child views.

The measure () process for each child view is implemented by calling the measurechildwithmargins () method in the parent viewgroup.java class, which simply calls the measure () method of the view object.

The whole measure calling process is a tree like recursive process

Both parameters of the measure () method are passed from the parent view, which represents the specification of the parent view. It consists of two parts. The upper two bits represent mode. It is defined in the measurespec class (the internal class of view). There are three types. Measurespec.actual represents determining the size, and measurespec.at_ Most indicates the maximum size, and measurespec.unspecified is uncertain. The lower 30 bits represent size, that is, the size of the parent view. For the decorview object of the system window class, the mode is generally measurespec.actual, and the size corresponds to the screen width and height respectively. For child views, the size is determined by both the parent view and the child view.

Layout layout process

Main function: to put the whole view tree in the appropriate position according to the size of the sub view and the layout parameters. The specific call is as follows: in the performTraversals method of ViewRootImpl, call performLayout.

Host. Layout () starts the layout of the view tree, and then calls back to the layout () method in the view / ViewGroup class. The specific process is as follows

1. The layout method will set that the view is located on the coordinate axis of the parent view, i.e. mleft, MTop, mleft and mbobottom (call the setframe() function to implement it), and then call back the onlayout() method (if the view is a ViewGroup object, you need to implement this method to layout each child view). 2. If the view is of ViewGroup type, you need to traverse each child view, and call the layout () method of the child view to set its coordinate value.

Draw () drawing process

In the performTraversals method of ViewRootImpl, the draw method of mView is called.

Mview. Draw() starts drawing. The functions realized by the draw() method are as follows:

1. Draw the background of the view

2. Do some preparation for displaying the gradient box

3. Call the OnDraw () method to draw the view itself (each view needs to overload this method, and the ViewGroup does not need to implement this method)

4. Call the dispatchdraw() method to draw a subview (if the view type is not ViewGroup, that is, it does not contain subviews, you do not need to overload this method)

It is worth noting that the ViewGroup class has rewritten the function implementation of dispatchdraw() for us. Generally, applications do not need to rewrite this method, but they can overload the parent class function to realize specific functions.

The dispatchdraw () method will traverse each subview internally, and call drawchild () to recall the draw () method of each subview.

5. Draw scroll bar

refresh the view

There are two methods to update the view in Android, one is invalidate, and the other is postinvalidate. The former is used in the UI thread itself, and the latter is used in non UI threads.

Requestlayout () method: this will cause the measure () procedure and the layout () procedure to be called.

Note: the layout process includes measure () and layout () processes. The draw () process will not be called, but will not be redrawn

Any view includes the caller itself.

Generally, the functions that cause the invalidate() operation are as follows:

1. Call the invalidate () method directly and request to draw () again, but only the caller itself will be drawn.

2. Setselection () method: request to redraw (), but only the caller itself will be drawn.

3. Setvisibility() method: when the visual state of the view changes from invisible to visible, it will indirectly call the invalidate() method, and then draw the view.

4. Setenabled () method: request redraw (), but no view will be redrawn, including the caller itself.

summary

The above is all about the detailed description of view drawing process in android in this article. I hope it will be helpful to you. If there are deficiencies, please leave a message to point out.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>