The process of drawing view on Android

Source: Internet
Author: User
Tags xml attribute

1 Android Drawing View simple description of the process


A simple description can be interpreted as: Calculate size (measure), Layout coordinate calculation (layouts), Draw to the screen (draw);
Let 's take a look at each step of the action exactly what it is,
The first step: When the activity starts, triggering the initialization of the view process is called by the Window object's Decorview call view (specifically how to read from the XML is in Layoutinflater.from (context). Inflat e) The public final void measure (int widthmeasurespec, int heightmeasurespec) method of the object is the final type, which means that all subclasses cannot inherit the method. The principle of Android initialization view is guaranteed to be the same. The specific parameter class value, which is described later.

The second step: view of the Measure method Onmeasure (Widthmeasurespec, Heightmeasurespec), the method for a substantial view size calculation. Note: The size of the view is determined by the parent view and its size, rather than the single decision. This is why ViewGroup's subclasses will be re-used, such as LinearLayout. Because they want to calculate the size of themselves and the child view. The view base class has its own implementation, just set the size. In fact, according to the source, the process of measure is essentially converting match_parent and wrap_content to actual size.

Step Three: When measure is finished, return to Decorview, calculate the size of the calculation, then start the layout, start calling the public final void layout of view (int l, int t, int r, int b), which is also final type, with the same purpose as the measure method. The layout method internally calls the OnLayout (int l, int t, int r, int b) method, and the second ViewGroup the method abstract, so we need to re-use this method when we inherit ViewGroup. The essence of this method is to calculate the coordinate point of view on the screen by calculating the measure size .

Fourth Step: measure, layout over, then began to draw to the screen, so began to call the view public void draw (canvas canvas) method, this time the method is not final, because the programmer can painting, internal calls to OnDraw, we often need to rewrite the method.

The above is the approximate working process of the view, of course, just outline, details more into the horse!!!!!

2 The process of measure

Public final void measure (int widthmeasurespec, int heightmeasurespec) parameter origin and meaning of the delegate

The two parameters are passed by the parent view, which represents the size of the parent view. In fact, the size is not quite right, it should be said to suggest "specifications." He has two parts,

The first part: The high 16-bit representation mode, defined in the Measurespec class, has three types,

(1) measurespec.exactly: Determines the size, the parent element determines the exact size of the child element, and the child element is confined to the given boundary and ignores its size

(2) Measurespec.at_most: Represents the maximum size at which the child element reaches a value of the specified size.

(3) Measurespec.unspecified: not sure. The parent element does not impose any constraints on the child elements, which can be arbitrarily sized to the desired size.

The second part: the low 16-bit represents the size of the parent view,

That's why we're rewriting the Onmeasure method is needed:

int specmode = Measurespec.getmode (spec);

int specsize = measurespec.getsize (spec);

This is called because Measurespec knows how to read. For root view (not the first element we declare in XML), it is the Decorview object of the system's Window object. Mode is generally measurespec.exactly, and the size corresponds to the screen width and height respectively. This is the first time that window was dropped (the view is the first element declared in XML), which is generally the value, and for a child view, this is the attribute android:layout_width and Android that you define in XML: Layout_height This, of course, said that the size of the view is the parent view and the child view together decided, so this is a bit wrong, but from the two values. The meaning is clear, let's see what measure is doing inside.

How is the Measurespec class used?

It is usually called in the Onmeasure method of the view component.

It is used by three functions:

1.static int getmode (int measurespec): Extract mode according to the measured value (format) provided (one of the three modes above)

2.static int getsize (int measurespec): Extracts the size value according to the measured value (format) provided (this size is what we usually call size)

3.static int makemeasurespec (int size,int mode): Creates a measure value (format) based on the provided size value and mode

3. Measure method Internal operation process

Call Onmeasure (Widthmeasurespec, Heightmeasurespec), pass the proposed size and specification of the parent view into the view class's own Onmeasure method, simply initialize the size based on the size configured in the XML. This is not analyzed, it is important that we look at the Onmeasure method in ViewGroup, the ViewGroup class does not handle the method, generally in his subclass processing, such as linearlayout, we use LinearLayout as the analysis class.
The Onmeasure method of the LinearLayout class is handled in two cases, 1: Reset sort, 2: horizontal sort, this everybody knows, we analyze reset sort,
• Get all sub-view numbers, start processing for each sub-view, and skip directly if the child view is gone
• In the Linearlaout.measurevertical method, first: Get the layoutparams of the child view, the parameters defined in the XML will be added to the variable totalweight by layout_weight defined values, All weights are then judged if the height of the view is set to zero, but the weight setting is greater than 0, then the value of height is set to layoutparams.wrap_content this value, the other is not processed
• Then call the Measurechildwithmargins method, which does the processing including: Calculate the measurespec of the sub view, that is, mode and size, call method: Getchildmeasurespec, call two times, respectively calculate width and height, Getchildmeasurespec calculates the measure of a child view internally based on the Layout_width and Layout_height properties of the parent view's measure and child view

Getchildmeasurespec calculates the measure of a sub-view, summarized as follows:

1. If the specific size of the child view is specified in the XML, the result is exacity+child_size regardless of the parent's measure.

2. If the height of the child view specifies Is fill_parent, the result is: Exacity+size, the reason is simple: because fill_parent means to fill the parent view, the measure of the returned child view is the size of the view!

3. If the size of the child vide is wrap_content, the returned result is at_most+size, because the maximum size of the parent view cannot be exceeded.

• After the measure of the sub-view is determined, then the measure method of the child view is called, and if the child view is the view object, the size of the view is measured and the next sub-view loop is started, and if the child view is ViewGroup then Start again with a new recursion, with the same processing logic as above, which is worth the end of all view object measurements.
LinearLayout will add the height of the child view to the variable mtotallength at the end of each of his direct sub-view measurements, which should actually be called mtotalheight more appropriate, but in order to be the same as Wight, so named this (this process does not handle : The size of the parent view is specified as a specific value and fill_parent, and the child view's height is specified as 0 and the child viewweight value is greater than.
• After all sub-view measurements are completed, the Layout_weight calculation is started, so we may think that if the parent view is already full, it is possible that the view object that is greater than 0 layout_weight is not displayed, and the calculation layout_ Weight method is also very simple, is to use the total height minus the above analysis of the value of Mtotallength, is the rest, and then split to the View object, pay attention to calculate the weight priority to android:android: WeightSum (linearlayout XML attribute) value, if not set this value will be calculated and, so since the value is set, it must be sub-view weight sum equal, otherwise the split may not get the desired effect, analysis an example:

<linearlayout android:layout_width= "Fill_parent"
android:layout_height= "200DP"
>
<textview android:layout_width= "Fill_parent"
android:layout_height= "100DP"
android:layout_weight= "1"
/>
<textview android:layout_width= "Fill_parent"
android:layout_height= "0DP"
android:layout_weight= "1"
/>
</LinearLayout>

The above example calculates the first TextView, according to the android:layout_height= "100DP" value, determines the height of 100DP, when the second textview is calculated, because android:layout_height= " 0 "is 0, so do not calculate its height, wait until the calculation of weight, when the calculation of weight, do not think the first TextView has been calculated, do not calculate, or calculate, the calculation process is as follows: the first assigned 100DP, still left 100DP, So two textview each point 50DP, so the first TextView 150DP, the second one is 50DP

At this point, the view's measure is over, all the view values are over, and you may find that this process only uses a few attributes: Android:layout_width,android:layout_height,android:layout_ Weight also has Marger and pading, and most of the other properties are used at OnDraw time,

The process of drawing view on Android

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.