Drawing process of Android view (Part I) measurement of view

overview

The drawing process of view can be divided into three steps: measure, layout and draw. Measure represents the measurement process of view, which is used to measure the width and height of view; Layout is used to determine the position of the view in the parent container; Draw is responsible for drawing the view to the screen. Let's take a look at the measure process of view.

measurement process

The drawing process of view starts with the performtraversals method of viewroot, which corresponds to the viewrootimpl class. Viewroot will call the performmeasure method in performtraversals to measure the root view. In the performmeasure method, the measure method of view will be called. For the measure method of view, it is a final type, that is, the measure method cannot be overridden by subclasses. However, the onMeasure method is invoked in the measure method. Therefore, subclasses of view can override the onmeasure method to implement their own measure procedures. Here, we mainly analyze the onmeasure method.

MeasureSpec

Measurespec is a static inner class in the view class. A measurespec encapsulates the layout requirements passed from the parent layout to the child layout. Each measurespec represents a height or width requirement. Each mesurespec is composed of specsize and specmode, which represents a 32-bit int value, in which the upper 2 bits represent specsize and the lower 30 bits represent specmode. There are three measurement modes of measurespec, which are described below:

The unspecified parent container has no restrictions on the child view, and the child view can be of any size. The actual parent container specifies a specific value for the size of the child view, and the final size of the view is specsize. Corresponding view attribute match_ Parent and specific values. AT_ The maximum size of the most subview can only be specsize, that is, the size of the subview cannot exceed specsize. Wrap corresponding to view property_ content.

In measurespec, you can create a measurespec through specsize and specmode and use the makemeasurespec method. You can also obtain the specmode and specsize of measurespec through getmode and getSize.

Measurement process of view

As mentioned above, the measure process of view is completed by the measure method, and the measure method completes the measure process of view by calling the onmeasure method. Let's take a look at the onmeasure method.

In the onmeasure method of view, only the setmeasureddimension method is called. The function of setmeasureddimension method is to set the measured values of height and width of view. For view, the measured width and height values are obtained through the getdefaultsize method. Here is the getdefaultsize method.

At for measurespec_ In most and actual modes, the specsize of measurespec is returned directly, that is, the specsize is the size measured by view. For unspecified mode, the measured value of view is the first parameter size in the getdefaultsize method. The width and height corresponding to this size are obtained through getsuggestedminimumwidth and getsuggestedminimumheight. Let's take a look at these two methods.

Here you can see that the values of view width and height are set according to whether the view has a background. Here, it is explained by the width of view. If the view has no background, the width of the view is mminwidth. For the minwidth value setting, you can set the minwidth property in the XML layout file, and its default value is 0. It can also be assigned by calling the setminimumwidth() method of view. If the view has a background, take the maximum of the minimum width of the view itself, minwidth, and the minimum width of the view background.

Measurement process of ViewGroup

For the measure process of ViewGroup, ViewGroup handles the size of the measure itself. It also needs to traverse the child views and call their measure methods, and then each child element recursively executes the measure process. The onmeasure method is not overridden in ViewGroup, because ViewGroup is an abstract class. For different concrete viewgroups, the processes implemented in its onmeasure method are different. However, a measurechildren method is provided in the ViewGroup to measure the child views. Let's take a look at the measurechildren method.

Get all the child views in the ViewGroup here. Then traverse the child views of the ViewGroup and call the measurechild method to complete the measurement of the child views. Let's take a look at the measurechild method.

In this code, get the measurespec of the width and height of the child view through the getchildmeasurespec method. The measure method of sub View is called to start measuring View. Let's take a look at how to obtain the measurespec of view through the getchildmeasurespec method.

In this code, the measurespec is obtained mainly according to the measurespec of the parent container and the layoutparams of the view itself. Let's look at the corresponding relationship between them through a table.

Here, after obtaining the measurespec of the child view through the getchildmeasurespec method, call the measure method of the view to start measuring the view. As just said, for ViewGroup, it is an abstract class and does not override the onmeasure method of view. However, when it comes to a specific ViewGroup, such as FrameLayout, LinearLayout, relativelayout, etc., they complete the measure process of themselves and their child views by overriding the onmeasure method. Let's take FrameLayout as an example to take a look at the measurement process. In FrameLayout, the measurement process is relatively simple. Let's take a look at the onmeasure method in FrameLayout.

In this part of the code, the logic is also very simple, mainly completing two things. Firstly, FrameLayout completes its own measurement process, and then after traversing the sub view, execute the measure method of the view to complete the measure process of the view. Here, the code is relatively simple and will not be described in detail.

summary

Finally, summarize the measure process of view and ViewGroup. For view, its measure is very simple. After obtaining the measured values of the height and width of the view, set the height and width. For the ViewGroup, in addition to completing its own measurement process, it also needs to traverse the sub views to complete the measurement process of the sub views.

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
分享
二维码
< <上一篇
下一篇>>