Summary of Android-view drawing principle

Source: Internet
Author: User

The design of the view structure of the Android system also adopts the combination mode, that is, the view as the base class of all the graphs, viewgroup the view inheritance into the views container class, thus obtains the basic structure of the view part-tree structure

View defines the basic operation of the drawing

The basic operation is done by three functions: measure (), layout (), Draw (), and its interior contains onmeasure (), OnLayout (), OnDraw () three sub-methods respectively. Here's how:

1. Measure operation

The measure operation is primarily used to calculate the size of the view, which is the width and length of the view. Defined as the final type in view, requires that the subclass cannot be modified. The following function is also called in the measure () function:

(1) onmeasure (), the size of the view will be finalized here, that is to say measure is just a wrapper for onmeasure, subclasses can overwrite the Onmeasure () method to achieve their own calculation of the view size of the way, The calculation results are saved by setmeasureddimension (width, height).

2. Layout operation

The layout action is used to set where the view appears on the screen. Defined as the final type in view, requires that the subclass cannot be modified. There are two basic operations in the layout () function:

(1) setframe (l,t,r,b), l,t,r,b the specific position of the child view in the parent view, which is used to save these parameters;

(2) OnLayout (), in view, this function does nothing, provided the function is mainly for the ViewGroup type layout sub-view;

3. Draw operation

The draw operation takes advantage of the parameters obtained from the first two parts, displays the view on the screen, and completes the entire view drawing work. The subclass should also not modify the method because it internally defines the basic operation of the drawing:

(1) drawing the background;

(2) If you want the view to display the gradient box, there will be some preparation work;

(3) Draw the view itself, called the OnDraw () function. OnDraw () is an empty function in view, which means that the specific view has to override the function to implement its own display (for example, the process of drawing text TextView here). For ViewGroup, you do not need to implement this function, because as a container is "no content", it contains multiple sub-view, and the child view has implemented its own drawing method, so only need to tell the child view to draw their own, that is, the following Dispatchdraw () Method

(4) Draw a sub-view, which is the Dispatchdraw () function. In view this is an empty function, the specific view does not need to implement the method, it is specially prepared for the container class, that is, the container class must implement the method;

(5) If necessary (the application calls Setverticalfadingedge or Sethorizontalfadingedge), start drawing the gradient box;

(6) Draw the scroll bar;

From the above you can see that custom view requires a minimum of two methods to overwrite Onmeasure () and OnDraw ().

ViewGroupThe extended operation in:

First ViewGroup is an abstract class.

1, the measure process of the child view

(1) Measurechildren (), internally using a For loop sub-view traversal, call the measure () method of the child view, respectively;

(2) Measurechild (), measure for the specified child view, will be called by Measurechildren;

(3) Measurechildwithmargins (), taking into account the measure of margin and padding for the specified sub-view;

The above three methods are viewgroup provided by the 3 sub-view to measure the reference method, the designer needs to overwrite the onmeasure () in practice, and then the sub-view to traverse measure, this time can use the above three methods, Of course, you can also customize the method to traverse.

2, the layout of the child view process

In ViewGroup, OnLayout () is defined as the abstract type, which means that the specific container must implement this method to arrange the layout of the child view, the main consideration is the size of the view and the relative position relationship between the views, such as gravity, Layout_ Gravity

3. Draw process for child views

(1) Dispatchdraw (), this method is used to traverse the child view and then separately draw the sub-view, the method will first handle the layout animation (that is, the layout animation is handled here), if there is a layout animation will create a drawing time for each child view, Then there is a for loop that iterates through the sub-view to invoke the draw method of the child view (actually the drawchild () below);

(2) Drawchild (), which is used to specifically invoke the draw method of a child view, which first processes the view animation (that is, where the view animation is handled), and then calls the Child View Draw ().

From the above analysis, it can be seen that the custom viewgroup need to write a minimum of onmeasure () and OnLayout () method, wherein the Onmeasure method can directly call Measurechildren and other existing methods, And the OnLayout method requires the designer to complete the definition, generally do not need to overwrite the Dispatchdraw () and Drawchild () These two methods, because the above two methods have completed the basic thing. However, it is possible to do some special effects on this basis by covering it, such as

@Overrideprotected void Dispatchdraw (canvas canvas) {//TODO auto-generated method stub////can do some processing here first, including the incoming canvas// Super.dispatchdraw (canvas);//This will call Drawchild to draw the child view////all the child views are drawn and there are some things you can do here, like drawing a shadow or something.

Other

From the above analysis, it can be seen that the drawing of view tree is a recursive process, from ViewGroup until all sub-view is finished drawing, then where is the source of all this (who initiated measure, layout and draw)? Of course it's at the source of the view tree--viewroot! , Viewroot contains the Performtraversal () method in the window's total container decorview,viewroot, which calls Decorview's measure, layout, draw method in turn to finish drawing the view tree.

Invalidate () method

The invalidate () method causes the view tree to redraw, and the status flag in view Mprivateflags has a flag bit drawn that the current view needs to be redrawn, that is, only the view with the flag bit drawn set will need to be redrawn. When the view calls the Invalidate () method, the drawn flag of the current view is first placed, followed by a loop call to Parent.invalidatechildinparent (), which causes the current view to traverse up and down until the root view Viewroot, this process will set the view marker drawn that need to be redrawn, and then viewroot call the Performtraversals () method to complete the drawing process of the view.

Reference book "Android Kernel Anatomy"--New Year's Day

Https://www.cnblogs.com/xgjblog/p/4128751.html

Summary of Android-view drawing principle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.