Translation How Android is drawing the view

Source: Internet
Author: User

Original: How Android draws views

When an activity gets to the focus, its layout begins to be drawn.

The process of drawing is handled by the Android framework. However, the root node of the layout hierarchy must be provided by the activity.

The drawing of the view begins with the root node of the layout, and the entire layout and its sub-layout (layout tree) are measured and plotted by traversing the layout trees and rendering the view intersecting each and every invalid area. In turn, ViewGroup's duty is to request that each of its child elements be drawn (through the Draw () method), while each view's duty is to draw themselves. (meaning that ViewGroup is only responsible for arranging the position, and view is responsible for drawing the work) because the elements are arranged sequentially, the parent element is always drawn before the child element, and the same level elements of the parent element are drawn in the order in which they appear.

The layout is drawn into two processes: the measurement process and the layout process.

The measurement process is implemented in the measure (Int,int) method. It is measured from top to bottom according to the arrangement of the view tree. At the end of the measurement process, each view holds its own size.

Layout procedures are implemented in the layout (int, int, int, int) method, which is also done from top to bottom. In this process, the parent node of the child element arranges their position according to the values that the child elements obtained in the previous step.

When a view's measure () method returns, the value of its getmeasuredwidth () and getmeasuredheight () must be set well. This child view must adhere to this restriction when the parent element of the view has a limit on the width and height of the child view. This ensures that the width and height of the sub-view can be accepted by the parent element at the end of the measurement process. A parent view may call multiple measure methods on its child view. For example, the parent view calls the measure () method for the first time to get the height of the child view request, and after the wide-high value is obtained, the parent view calls the measure method again based on these values to detect whether the values are beyond the limit (too large or too small), and if so, Then the parent view will be restricted in the second procedure.

During the measurement, child elements can use the Viewgroup.layoutparams class to inform the parent element of the dimensions they requested.

Viewgroup.layoutparams consists of two values:

Match_parent: Child elements need to be as large as parent elements (minus filler [padding])

Wrap_content: Child elements only need to be able to place their contents (plus padding)

ViewGroup different subclasses have different layoutparams. For example, Relativelayout also has its own Layoutparams class.

This allows the relativelayout to place child elements in the center horizontally and vertically.

The parent element can limit the size of child elements through the Measurespec class. The value of Measurespec can be one of the following three.

UNSPECIFIED: Used only to get the value requested by a child element, not to set a restriction on the sub element. For example: LinearLayout can set the length and width of its child elements to unspecified and exactly with a value of 240, so as to know how much height it will need when the length of the child element is 240px.

Exactly: The parent element restricts the value of the child element to the specified value. The child element must use this value and ensure that all its descendant elements are available within this dimension.

At_most: The parent element specifies the maximum size of the child element. The child element must ensure that it and all its descendant elements are available within this size.

Note: The Android framework does not draw a view that is not in an invalid area and is responsible for drawing the view background.

We can force a view to redraw by calling the invalidate () method.

initialize the layout by calling Requestlayout (). This method is called when a View object thinks that the current area is not working properly.

About valid and invalid zones: An invalid area is an area that needs to be updated, and the active area does not need to be updated.

Anyone with Windows development experience should know that when a window is obscured to appear, the window is redrawn, and the obscured area is an invalid area. Because when the region reappears, we have to make sure its content is up to date, so it needs to be updated to redraw.

All valid zones are known as active zones because the current state is already up-to-date and does not need to be updated, so it does not need to be redrawn.

Translation How Android is drawing the view

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.