How Android draws a view

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.

Reference: SDK documentation http://developer.android.com/guide/topics/ui/how-android-draws.html

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.