Android user interface-How Android draws views)

Source: Internet
Author: User

When the activity receives the focus, it will request to describe its layout. The android framework will deal with this plotting process, but the activity must provide its layout hierarchy and nodes.

It starts from the root node of the layout. It must measure and describe the layout tree. Plotting is done by traversing the entire layout tree, and each view object that is intersecting with the invalid area is displayed during the traversal process. Each viewgroup is responsible for requesting each sub-view to draw (using the draw () method), and each view object is responsible for drawing itself. Because the layout tree is traversed in sequence, this means that the parent node will be drawn before its child nodes, and the nodes at the same level will be drawn according to their display order in the tree.

There are two steps to describe the layout: Measurement and layout. The measurement process is implemented in the measure (INT, INT) method. It is a top-down traversal process. During recursion, each view transmits its size and specification down. At the end of the measurement process, each view object will save their own size. The layout process occurs in layout (INT,
Int, Int, INT) method, and it is also a top-down process. During this process, each parent container uses the size calculated during the measurement process to locate its sub-objects.

When the measure () method of a view object is returned, its getmeasuredwidth () and getmeasuredheight () methods (including all the sub-objects contained in this view object) must be meaningful. The measurement width and height of a view object must strictly abide by the constraints of its parent container. This ensures that after the measurement process ends, all parent containers can receive the size of all their child nodes. A parent view object can call its subnode's measure () method multiple times. For example, before dimensions are not measured, the parent view measures each child node to find the size they need, if the size of all its child nodes is too large or too small before being constrained, the parent node will call the measure () method once, and allocate the actual size to them (if the space obtained by the Child Nodes cannot meet their own needs, then the parent node will intervene and set the rules, and then perform the second traversal ).

The measurement process uses two classes to pass dimensions. The view. measurespec class is used to tell their parent nodes how they want to measure and locate them. The layoutparams class describes the width and height of a view object. You can specify each dimension in one of the following ways:

1. Precise Numbers

2. fill_parent: this means that the view object is as big as its parent container (minus the margin)

3. wrap_content: this means that the view object only wants the space (plus the margin) that matches its content)

Different viewgroup sub-classes have corresponding layoutparams sub-classes. For example, relativelayout has its own layoutparams sub-class, which includes the ability to center its sub-views horizontally and vertically.

The measurespecs class is used to send requests from the parent node down to the child node. Measurespec can be one of the following three modes:

1. Unspecified:

The parent node uses this mode to determine the desired size of its child node. For example, linearlayout can call the measure () method of a subnode whose height is set to unspecified and whose width is set to exactly 240 to measure the height of a view object whose width is 240 pixels.

2. Exactly:

The parent node uses this mode to forcibly set the precise size of the child node. The child node must use this size and make sure all its child nodes are fit for this size.

3. at_most:

The parent node uses this mode to force the child node to use the maximum size. The Byte must ensure that it and all its subnodes adapt to this size.

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.