View rendering in Android

Source: Internet
Author: User

When an activity receives a focus, it is required to draw its layout. The android framework will deal with this painting process, but the activity must provide its layout level root node.

Painting starts from the root node of the layout. It is required to measure and plot the layout tree. Painting is processed by traversing the layout tree and rendering the view that intersection each of the failure areas. Corresponding,Each view group is responsible for requesting to draw its subview (via the draw () method) and each view is responsible for drawing its own.Because the tree is traversed sequentially, this means that the parent node is first drawn (that is, behind the screen), and then the nodes at the same level are drawn according to the order in which the tree appears.

The frame will not draw a view that is not in the failed area, but will also help you draw a view background.

You can force a view to be repainted by callingInvalidate ().

There are two steps for painting layout: A measurement process and a layout process.. The measurement process is implemented in measure (INT, INT) and is a top-down view tree traversal. Each view pushes down the Size specification in recursion. At the end of the measurement process, each view has saved its own measurement. The second process occurs in layout (INT, Int, Int, INT) and is also top-down. In this process, each parent node is responsible for locating all its sub-nodes and using the size calculated during the measurement process.

When the measure () method of a view is returned, its getmeasuredwidth () and getmeasuredheight () values must be set, and the values of all the subnodes of this view. The width and height of a measurement in a view must comply with the restrictions introduced by the parent view. This ensures that after the measurement process, all parent nodes accept the measurement values of all their child nodes. A parent view may call the measure () method multiple times on its child views. For example, the parent view may call measure through unspecified dimensions to find their sizes, and then call Measure () again using actual values (), if the size of all subviews that are not limited is too large or too small (that is, if the subviews cannot reach a consensus on the space occupied by them, the parent view will intervene and set the rules for the second process ).

To start a layout, you can call requestlayout (). This method is usually called when the view thinks it is no longer suitable for its current boundary.

The measurement process uses two classes to exchange dimensions.The view. measurespec class is used by views to tell their parent views how they want to be measured and located. The basic layoutparams class only describes the size (height and width) of a view). For each dimension, it can specify one of the following:

    • · An accurate value
    • ·Fill_parent,This means that the view is as big as the parent view (PAD filling is removed ).
    • ·Wrap_contentThis means that the view only needs to be as big as the content that just wraps it (plus filling)

Different viewgroup subclasses have corresponding layoutparams subclasses. For example, relativelayout has its own layoutparams subclass, which includes the ability to display the child views horizontally and vertically in the center.

Measurespecs)It is used to transmit metric requirements from parent to child along the tree. One measurespecs can be one of the following three modes:

    • ·Unspecified: This is used by the parent view to determine the expected size of its child view. For example, a linear layout may call Measure () on its child on its sub-view, by setting its height to unspecified and a width of exactly 240, to find out how high the sub-view needs to be displayed with a given 240 pixel width.
    • ·Exactly: This is used by the parent view to impose an exact size on the Child view. The child view must use this size and make sure all its descendants will fit this size.
    • ·At_most: This is used by the parent view to impose a maximum size on the Child view. Make sure that the child view is fit for itself and all its descendants.

 

 

I. Also
Many children's shoes may not understand the usage of getwidth () and getmeasuredwidth (). What are the differences between the two? There are different versions on the Internet, however
Most of the data is in the same region. From this place Ctrl + C to another place Ctrl + V, there is no clear question, there are also someArticleI am also deeply affected by the recognition of these two methods.
In this case, I want to explain a lot of statements in the following version. Baidu has searched a lot. Unfortunately, this statement is not correct, so I hope that you will not blindly try again in your space:
Getwidth indicates the actual size of a view.
Getmeasuredwidth indicates the size of a view in the parent view.
Surely you have seen such a solution. It seems that such a solution is also within the classroom, and you have not understood the problem.

II. Okay, the zookeeper version is not mentioned much. Let's make a positive solution to these two methods. First, we should know the following points:
1. When a class is initialized, we cannot get the actual size of the view in the structure function. If you are interested, you can refer to getwidth () and getmeasuredwidth (). The result is 0, but we can get the control size from the ondraw () method.
2. The result of the two methods is pixel.
Introduce the two methods:
  Getwidth (): The result shows the degree of view after the parent layout is properly configured in the parent layout. If there is no parent board, then the entire screen is recognized as the parent board. It may be hard to understand. Let's illustrate through an example.
Example 1:
Public class test extends activity {
Private linearlayout mbackgroundlayout;
Private textviewtest mtextviewtest;

/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );

Mbackgroundlayout = new mylayout (this );
Mbackgroundlayout. setlayoutparams (New linearlayout. layoutparams (
Linearlayout. layoutparams. fill_parent,
Linearlayout. layoutparams. fill_parent ));

Mtextviewtest = new textviewtest (this );

Mbackgroundlayout. addview (mtextviewtest );
Setcontentview (mbackgroundlayout );
}
Public class mylayout extends linearlayout {

Public mylayout (context ){
Super (context );
// Todo auto-generated constructor stub
}

@ Override
Protected void onlayout (Boolean changed, int L, int T, int R, int B ){
// Todo auto-generated method stub
Super. onlayout (changed, L, t, R, B );
Log. I ("tag ","--------------");
View mview = getchildat (0 );
Mview. Measure (0, 0 );
}

}
Public class textviewtest extends textview {
Public textviewtest (context ){
Super (context );
// Todo auto-generated constructor stub
Settext ("test ");
}

@ Override
Protected void ondraw (canvas ){
// Todo auto-generated method stub
Super. ondraw (canvas );
// Measure (0, 0 );
Log. I ("tag", "width:" + getwidth () + ", height:" + getheight ());
Log. I ("tag", "measuredwidth:" + getmeasuredwidth ()
+ ", Measuredheight:" + getmeasuredheight ());
}

}
}
Add a textview control in linearlayout. If you want to get getwidth () for the textview cursor at this time, then add the textview to layout and set the value again, it is not a matter of order to take the margin of the textview itself.
Getmeasuredwidth (): Let's take a look at what the API says.
The
Width of this view as measured in the most recent call to measure ().
This shoshould be used during measurement and layout calculations only.
The result is the view degree obtained after the latest increment using the measure () method, which is used in the calculation of the increment volume and layout.
Therefore, this method is used to obtain the actual attention of the View content manager.
If you want to change from the simplest example to their differences, we will modify the above example as follows:
Public class test extends activity {
Private textviewtest mtextviewtest;

/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Mtextviewtest = new textviewtest (this );
Setcontentview (mtextviewtest );
}

Public class textviewtest extends textview {
Public textviewtest (context ){
Super (context );
// Todo auto-generated constructor stub
Settext ("test ");
}

@ Override
Protected void ondraw (canvas ){
// Todo auto-generated method stub
Super. ondraw (canvas );
Measure (0, 0 );
Log. I ("tag", "width:" + getwidth () + ", height:" + getheight ());
Log. I ("tag", "measuredwidth:" + getmeasuredwidth ()
+ ", Measuredheight:" + getmeasuredheight ());
}
}
}
Summary (positive solution ):
  Getwidth (): Specifies the overall view degree after setting the resolution.
 
Getmeasuredwidth ():
The consistency of the View content after the volume of content on the view is calculated, provided that you have to onlayout () method or in the ondraw () method of this view
Use measure (); (you can define the value of measure values). Otherwise, the result is the same as that obtained by getwidth.
Maybe my organization is not very good. If you have any questions, leave a message for me. The difference between the two methods is that you have not used the measure () method. Of course, the location of measure () is also very important.
3. Respect the original website. Please note that this is Http://hi.baidu.com/ljlkings/home .

 

-------------------------------------------------------------------- 2011/03
/01 Update ------------------------------------------------------------

1. What does layout_weight mean in XML?

A: the smaller the permission value, the higher the permission value, that is, the heavier the weight of the weight in the local authority.

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android =" Http://schemas.android.com/apk/res/android "
Android: Orientation = "horizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_weight = "1"
Android: text = "button1"
/>
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_weight = "2"
Android: text = "button2"
/>
</Linearlayout>

Because the minimum authority of button1 is set, the larger the authority it uses,This configuration means that the forward direction is divided into three parts, button1 for two parts, and button2 for one, which is very simple, you can leave a message if you do not understand anything. Thank you !,

See the effect below:

* ****** Modification

First of all, thank you.SunwayforeverTo avoid the further spread of my mistakes in this article, I would like to quote one sentence here: due to the limited level of the author, there will inevitably be omissions and errors in this article. I urge you to criticize and correct them. Thank you! (Note: I hope you can continue with the discussion above.)
This
To layout_weight
Let's talk about our new insights. First, we have a saying: "because the weight of button1 is set to the minimum, the layout occupied is larger ."
Fill_parent is correct, but if it is set to wrap_content, this sentence cannot be explained clearly. below isSunwayforeverUnderstanding of this attribute:

When linearlayout contains weight Child, linearlayout will be measure twice:
Set screen width to X
First time: the measuredwidth of button1 is X, and the value of button2 is X (because weight is used, linearlayout does not consider the previous occupied size for each measure child). total_width is 2X
Second: Calculate Delta = x-total_width =-X, and then set the width of button1
X + Delta * 1/3 = 0.66x, button2 is x + Delta * 2/3 = 0.33x

Now I want to repeat this sentence: "because the minimum weight of button1 is set, the layout priority is higher ", maybe there is no priority in the layout of Android. I just want to illustrate the problem and define it myself, so don't make a brick.

analyze the layout_width setting to fill_parent, that is, when the parent board is filled, of course, this control requires that the root setting of weight is possible
. Therefore, based on the above example, weight of button1 is set to 1, set weight of button2 to 2. that is to say, the priority of the button is the highest. Therefore, to fill the parent board,
button1 needs to be filled first. The priority may be large. What is the possibility of this priority, in this case, we need to combine the weight values of other controls in layout, and then perform the calculation, button1
adjust threshold 2/3, button2 limit 1/3. you can also set button2 to a very large number, such as 2000. At this time, in graphical
Layout mode, we can see that button1 is filled with the entire degree of parallelism, you can't see the shadow of button2. Actually, button2 still exists. You can point the mouse pointer to the backend of
button1 to see a growth condition, that is button2, It's already very small. therefore, when layout_width is set to fill_parent, weight indicates that your control needs to be scaled first.


when layout_weight is set to wrap_content, it indicates the response of the content, it means that this control may be small, as long as the content can be displayed
, the same, if you set the layout_weight of button1 and button2 to wrap_content, weight of button1 is
1, and weight of button2 is 2. so button1 needs to begin with a smaller value, while button2 also needs a smaller value, but the difference is that the values are different because the
weight, therefore, the overall coverage of these two controls must be set to the coverage of the parent board. Therefore, we need to calculate the size of the memory size of each control. At this time, button1 has two parts, one
1/3, one 2/3, And the other 1/3. button1 has to be renewed, so button1 needs to select. Therefore, instead, we can see the effect of button2 zookeeper. Large.
If the permission value is set as follows: button1 is 1 and button2 is 2000, does button1 have to allocate a blank space of 1/2000? This understanding is complete, just now
said, the limit may be small, but there is a limit
that is, wrap_content, that is, if the content is fully displayed, in the same way, there is also a limit for the possibility of growth, that is, the degree of consistency of the Parent Board. Therefore, when
layout_width is set to wrap_content, weight indicates that your control must be larger first.

Therefore, to understand weight, we need to deeply understand the following two sentences:
When layout_width is set to fill_parent, layout_weight indicates that your control needs to be scaled first, but this is limited, that is, fill_parent.
When layout_width is set to wrap_content, layout_weight indicates that your control needs to be scaled first, but this is limited, that is, wrap_content.

Layout_height is the same as layout_width.

 

The following example has been completed successfully:

1. layout_width = "fill_parent", weight of button1 = 1, weight of button2 = 2;


2. layout_width = "fill_parent", weight of button1 = 1, weight of button2 = 2000;


3. layout_width = "wrap_content", weight of button1 = 1, weight of button2 = 2;


4. layout_width = "wrap_content", weight of button1 = 1, weight of button2 = 2000;

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.