Android custom View and its example in layout file (2)

Source: Internet
Author: User

 

 

 

In the previous article (Android custom View and Its Use examples in layout files), we introduced that when the control provided by the system does not meet the needs of developers, demonstrate how to customize the View. This article will be a continuation of the previous article. First, we will introduce how Android draws the interface, which is a prerequisite theoretical basis, the next article will focus on several important methods in the Android painting interface process, such:
123,onDraw()
The theoretical basis of Android plotting: 1. We create an Activity to test the custom View in the previous article:

A)

CustomViewActivity. java

   CustomViewActivity                            }

B)

Customview_layout.xml

                               

C) running result:

2. How does Android achieve this?

A)

View the google documentation, which provides the following explanation:

When an Activity receives focus, it will be requested to draw its layout. The Android framework will handle the procedure for drawing, but the Activity must provide the root node of its layout hierarchy.

In combination with the example above, the Activity gets the focus when it enters the CustomViewActivity. At this time, the Activity will request the system to draw its layout, this request is processed using the Android framework, provided that CustomViewActivity must provide the root node of the layout, from CustomViewActivity. java shows that this Activity provides R. layout. customview_layout, and the root node of the layout is the LinearLayout of our layout file;

B)

   Drawing begins with the root node of the layout. It is requested to measure and draw the layout tree. Drawing is handled by walking the tree and rendering each View that intersects the invalid region. In turn, each ViewGroup is responsible for requesting each of its children to be drawn (with the draw() method) and each View is responsible for drawing itself. Because the tree is traversed in-order, this means that parents will be drawn before (i.e., behind) their children, with siblings drawn in the order they appear in the tree.

From this section, we can see that in Android, the View is drawn from the root node of the layout. Before the start, we need to traverse the entire layout structure. For example, ViewGroup and ViewGroup (such as LinearLayout, relativeLayout) needs to traverse and draw all its child views. If it is only a common View (TextView, etc.), it is only responsible for drawing itself.

C)

   Drawing the layout is a two pass process: a measure pass and a layout pass. The measuring pass is implemented in measure(int, int) and is a top-down traversal of the View tree. Each View pushes dimension specifications down the tree during the recursion. At the end of the measure pass, every View has stored its measurements. The second pass happens in layout(int, int, int, int)and is also top-down. During this pass each parent is responsible for positioning all of its children using the sizes computed in the measure pass.

Two steps are required for plotting. One is the measure (size measurement) process and the other is the layout (Location Calculation) process:

1) measure process: it needs to be implemented in the measure () method and traverse the tree structure of the measurement View from top to bottom. After the measurement, the measurement specifications (specifications) of each node are stored in this tree structure;

2) layout Process: by calling the layout (int, int) method, each parent View is responsible for the position of the child View, and the final size of the Child View, it is the size calculated through the measure process.

D)

         When a View object's measure() method returns, its getMeasuredWidth() and getMeasuredHeight() values must be set, along with those for all of that View object's descendants. A View object's measured width and measured height values must respect the constraints imposed by the View object's parents. This guarantees that at the end of the measure pass, all parents accept all of their children's measurements. A parent View may call measure() more than once on its children. For example, the parent may measure each child once with unspecified dimensions to find out how big they want to be, then call measure() on them again with actual numbers if the sum of all the children's unconstrained sizes is too big or too small (that is, if the children don't agree among themselves as to how much space they each get, the parent will intervene and set the rules on the second pass).

When the measure () method of each View object is returned, the measurement width and height values of each View must have been set, the two values are obtained by interacting with the parent View of the View object. This does not mean that each View object can request any desired value, if the requested width or height of the View object is unreasonable, the parent View of the View object will call the measure () method again to determine the final width and height of the View object, this will be explained in the detailed description of the onMeasure () process;

E)

2)MATCH_PARENT: 
which means the View wants to be as big as its parent (minus padding)3)WRAP_CONTENT:
  which means that the View wants to be just big enough to enclose its content (plus padding).There are subclasses of ViewGroup.LayoutParams for different subclasses of ViewGroup. For example, RelativeLayout has its own subclass of ViewGroup.LayoutParams, which includes the ability to center child View objects horizontally and vertically.MeasureSpec objects are used to push requirements down the tree from parent to child. A MeasureSpec can be in one of three modes:UNSPECIFIED: This is used by a parent to determine the desired dimension of a child View. For example, a LinearLayout may call measure() on its child with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how tall the child View wants to be given a width of 240 pixels.EXACTLY: This is used by the parent to impose an exact size on the child. The child must use this size, and guarantee that all of its descendants will fit within this size.AT MOST: This is used by the parent to impose a maximum size on the child. The child must guarantee that it and all of its descendants will fit within this size.

The measurement process uses two classes to communicate with dimensions (dimensions), which are ViewGroup. LayoutParams and MeasureSpec.

ViewGroup. LayoutParams:

The View object is used to tell its parent View how it is measured and where it is placed. However, ViewGroup. layoutParams only unilaterally describes the width and height it wants. It is not the final width and height drawn by ViewGroup. layoutParams can be specified as the following values:

1) an exact number:
The specific value; 2) MATCH_PARENT:
The same size as the parent container; 3) WRAP_CONTENT:
The size of the View depends on the content of the View. For example, in TextView, if the width is set to wrap_content, the width of the View is changed with the length of the text.

MeasureSpec:

This object encapsulates the layout requirements for the parent container to pass to the child element. It has three modes:

1)
UNSPECIFIED: the parent container has no requirements on child elements. The child element can obtain any value;
2)
EXACTLY: determines the size of the child element in the parent window. The child element is limited to the given boundary and its size is ignored;
3)
At most: the size of the child element reaches the maximum size specified in the parent window. The child element cannot exceed this boundary;
 


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.