Android rendering View

Source: Internet
Author: User

 

When an Activity gets the focus, it requests to draw its layout. The Android framework manages the painting process, but the Activity must provide the root node in the layout hierarchy.

The view is drawn from the root node of the layout. Calculate and draw the layout tree. Rendering is responsible for traversing the tree and rendering each invalid area that can interact. In turn, each View group is responsible for requesting to draw its subviews, and each View is responsible for drawing itself (using the draw () method ). Because the tree is traversed sequentially, this means that the parent node is first drawn (the parent View is behind the child View), and other peer nodes are drawn according to the order of the tree.

The Android framework only re-draws the invalid region and helps you draw background images.

You can use the invalidate () method to forcibly redraw the View.

Drawing layout is two transfer processes: one measurement transfer and one layout transfer. Measurement transfer is to implement the measure (int, int) method and traverse the view tree from top to bottom. Each view transmits the Size specification in recursion. At the end of the size transfer, each view stores the size. The second transfer occurs in layout (int, int), which is also top-down. During this transfer period, each parent node is responsible for calculating the size of the measurement data and locating its child nodes.

When the measure () method of View is returned, the value of getMeasuredWidth () and getMeasuredHeight () must be set, and the values of all View descendants. The value of the measurement width and height of the View must comply with the constraints imposed by the View's parent node. This ensures that all parent nodes accept the size values of their child nodes at the end of Measurement transfer. A parent node can call the measure () method multiple times for its child nodes. For example, a parent node can call the measure () method to identify the desired size of each subnode with an ambiguous size, and then call the measure () method using the actual value, if the total size of all the child nodes that are not constrained is too large or too small (that is, if the child node does not agree with the space between each other, the parent node will intervene and set the second-stage rules ).

Update a layout and call requestLayout (). This method is usually called when the View thinks it is not suitable for the current layout.

Measurement transfer uses two classes to communicate dimensions.

· TheView. MeasureSpec class is usually used for View to tell their parent nodes how they want to calculate and locate.

· The basic LayoutParams class only describes the length and width of a View. Each dimension can be specified:

& A definite value

& FILL_PARENT, indicating that the View is the same size as the parent node (minus filling)

& WRAP_CONTENT, indicating that the View only wants to accommodate its content size (plus filling)

 

Each ViewGroup subclass has a corresponding LayoutParams subclass. For example, RelativeLayout has its own LayoutParams, including the ability to center the child views horizontally and vertically.

MeasureSpecs is usually used to transmit data from the parent node to the child node in the tree. MeasureSpec can be one of the three modes:

& UNSPECIFIED: it is usually used by the parent node to determine the size of the child node. For example, LinearLayout may call the measure () method on its subnodes and set its height to UNSPECIFIED and Its width to EXACTLY 240 to locate the height of the subview when its width is 240 pixels.

& EXACTLY: usually used for parent nodes to take advantage of an absolute size of child. Child must use this size and ensure that all child nodes will fit this size.

& AT_MOST: usually used for parent to take advantage of the maximum size of child. This child must ensure that all its child nodes are fit in this size.

Author: ldj299

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.