Android draws a view. android draws a view.

Source: Internet
Author: User
Tags xml attribute

Android draws a view. android draws a view.

1. simple description of the process for android to draw a view


A simple description can be interpreted as: calculation size (measure), layout coordinate calculation (layout), and drawing to the screen (draw );
Next, let's take a look at the actions in each step,
Step 1: when the activity is started, the view is called by the DecorView of the Window object during initialization (LayoutInflater is used to read from xml. from (context ). inflate) The public final void measure (int widthMeasureSpec, int heightMeasureSpec) method of the object starts. This method is of the final type, that is, all subclasses cannot inherit this method, ensure that the principle of the android initialization view remains unchanged. The specific parameter class value will be described later.

Step 2: View's measure method onMeasure (widthMeasureSpec, heightMeasureSpec). This method computes the actual view size. Note: The size of a view is determined by its parent view and its own size, rather than a single one. This is why the subclass of ViewGroup reuses this method, such as LinearLayout. Because they need to calculate the size of themselves and the Child view. The View base class has its own implementation, but only sets the size. In fact, according to the source code, the measure process is essentially converting Match_parent and wrap_content to the actual size.

Step 3: When measure ends, return to DecorView. After the calculation is completed, the layout begins. Call the public final void layout (int l, int t, int r, int B), this is still final type, with the same purpose as the measure method. The layout method calls the onlayout (int l, int t, int r, int B) method internally. The second ViewGroup abstract The method, so when we inherit the ViewGroup, you need to repeat this method. The essence of this method is to calculate the size and coordinate points of the view on the screen through the measure calculation.

Step 4: If measure is over and layout is over, it will be drawn to the screen. Therefore, the public void draw (Canvas canvas) method of the view is called. At this time, the method is not final, the reason is that programmers can draw their own images and call ondraw internally. We often need to rewrite the method.

The above is the general working process of the view. Of course, it's just an overview. The details are too much !!!!!

 

2. measure Process

Public final void measure (int widthMeasureSpec, int heightMeasureSpec) parameter source and meaning

The two parameters are passed by the parent view, that is, the size of the parent view. In fact, the size is not very correct. It should be said that the "Specification" is recommended ". It consists of two parts,

Part 1: the high 16-Bit MODE is defined in the MeasureSpec class. There are three types,

(1) MeasureSpec. EXACTLY: determines the size. The exact size of the parent element determines the size of the child element. The child element is limited to the given boundary and its size is ignored.

(2) MeasureSpec. AT_MOST: indicates the maximum size. The child element can reach the specified size at most.

(3) MeasureSpec. UNSPECIFIED: Not sure. The parent element does not apply any constraint to the child element. The child element can be of any desired size.

 

Part 2: A low 16-bit value indicates the size of the parent view,

This is why we need to rewrite the onmeasure method:

Int specMode = MeasureSpec. getMode (spec );

Int specSize = MeasureSpec. getSize (spec );

This is called because MeasureSpec knows how to read data. For the Root view (not the first element we declare in xml), it is the decorVIew object of the system Window object. Mode is usually MeasureSpec. EXACTLY, and size corresponds to the screen width and height respectively. That is, the view used by the Window for the first time (this view is the first element declared in Xml). Generally, this value is used. For the sub-view, the connection value is the property android: layout_width and android: layout_height defined in xml. The size of the view is determined by the parent view and the Child view, so this is a bit wrong, but it comes from these two values. Now, let's see what is done in measure.

 

How to Use the MeasureSpec class?

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

It has three common functions:

 

1. static int getMode (int measureSpec): extract the mode based on the provided measured value (Format) (one of the preceding three modes)

2. static int getSize (int measureSpec): extract the size value based on the provided measured value (Format) (this size is what we usually call the size)

3. static int makeMeasureSpec (int size, int mode): Create a measurement value based on the provided size value and mode (Format)

 

3. Internal Operation Procedure of the measure Method

Call onmeasurespec (widthMeasureSpec, heightMeasureSpec) to pass in the recommended size and specification of the parent view. The onMeasure method of the view class is only used to initialize the size according to the size configured in xml. This will not be analyzed, important: Let's take a look at the onMeasure method in viewGroup. The ViewGroup class does not process this method. It is generally processed in its subclass. For example, in LinearLayout, we use Linearlayout as the analysis class.
• The onMeasure method of the Linearlayout class can be processed in two cases: 1: reset sorting and 2: horizontal sorting. As we all know, we analyze and reset sorting,
• Obtain the number of all sub-views and start processing each sub-view. If the sub-view is GONE, skip it directly.
• In LinearLaout. in the measureVertical method, first obtain the LayoutParams of the sub-view. In the parameters defined in xml, the value defined by layout_weight is accumulated into the totalWeight variable, with all the weights, then, if the view height is set to zero but weight is greater than 0, the value of height is set to LayoutParams. the value of WRAP_CONTENT is not processed by others.
• Call the measureChildWithMargins method to calculate the measureSpec of the sub-view, that is, the MODE and SIZE. The call method is getChildMeasureSpec, which is called twice to calculate the width and height respectively, getChildMeasureSpec internally calculates the Measure of the sub-view based on the measure of the parent view and the layout_width and layout_height attributes of the sub-view.

GetChildMeasureSpec:

1. If the specific size of the Child view is specified in xml, the calculation result is EXACITY + child_size regardless of the parent measure,

2. if the value specified by the Child view height is FILL_PARENT, the returned result is: EXACITY + size. The reason is simple: because FILL_PARENT indicates that the parent view is full, therefore, the returned sub-view measure is the size of the view!

3. If the size of the sub-vide is wrap_content, all returned results are At_most + size, because the maximum size of the parent view cannot exceed.

• After the measurement of the sub-view is determined, call the measure method of the sub-view. If the sub-view is a View object, the size measurement of the view ends and the next sub-view cycle starts, if the sub-view is a ViewGroup, a new recursion is started. The processing logic is the same as that above. It is worth all the view object measurement ends.
• LinearLayout accumulates the height of the Child view to the variable mTotalLength after each direct sub-view measurement is completed. It should be called mTotalHeight, but in order to be the same as wight, therefore, name it as this (this process is not handled: the size of the parent view is specified as the specific value and fill_parent, and the height of the Child view is specified as 0 and the Child viewweight value is greater ).
• The layout_weight calculation is started only after all the child views are measured. In this way, we may think that if the parent view is full, it is possible that the view object with layout_weight greater than 0 will not be displayed, but the layout_weight calculation method is also very simple, that is, the total height minus the mTotalLength value analyzed above, that is, the rest, then divide the value to the view object. When calculating the weight, the value of android: weightSum (xml Attribute of LinearLayout) is prioritized. If this value is not set, therefore, since this value is set, the sum of weight Values of the sub-view must be equal. Otherwise, the split may not produce the expected results. Let's analyze 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>

In the preceding example, when calculating the first TextView, the height is determined to be 100dp Based on the android: layout_height = "100dp" value. When calculating the second TextView, because android: layout_height = "0" is 0, so the height is not calculated. It is calculated only when weight is calculated. When weight is calculated, never think that the first TextView has been computed, the calculation process is as follows: the first one is allocated 100dp with 100dp left. Therefore, the two textviews are divided into 50dp, so the first textview has 150dp, the second is 50dp.

So far, the measurement of the view is over, and all the view values are over. You may find that this process uses only a few attributes: android: layout_width, android: layout_height, android: layout_weight includes marger and pading. Most other attributes are used in ondraw,

 

 

 

 

 

 

 

 

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.