[Translation] How does Android draw a view? How does android draw a view?
Original article: How Android Draws Views
When an Activity gets the focus, its layout is drawn.
The rendering process is handled by the Android framework. However, the layout-level root node must be provided by Activity.
A view is drawn from the root node of the layout. The layout and its sub-layout (layout tree) are traversed by traversing the layout tree and rendering the view that crosses each and invalid areas) will be measured and drawn. In turn, ViewGroup is responsible for requesting that each of its child elements be drawn (via the draw () method), and each View is responsible for drawing itself. (ViewGroup is only responsible for ranking, while view is responsible for plotting.) Because elements are arranged in order, parent elements are always drawn prior to child elements, the same level elements of the parent element are drawn in the order they appear.
There are two steps to draw the layout: 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 view tree arrangement. After the measurement process is complete, each View stores its own size.
The layout process is implemented in the layout (int, int) method. It is also implemented from top to bottom. In this process, the parent node of the child element arranges their positions based on the value obtained by the child element in the previous step.
When a View's measure () method returns, its getMeasuredWidth () and getMeasuredHeight () values must be set. When the parent element of a View has a limit on the width and height of the Child View, the Child View must comply with this restriction. This ensures that the wide and high values of the Child View can be accepted by the parent element at the end of the measurement process. A parent View may call the measure method multiple times for its child View. For example, a parent View calls the measure () method for the first time to obtain the width and height of the Child View request. After obtaining the width and height values, the parent View calls the measure method again based on these values, then, check whether these values exceed the limit (too large or too small). If yes, the parent View will limit the value in the second process.
During the measurement process, the child element can use the ViewGroup. LayoutParams class to notify the parent element of the requested size.
ViewGroup. LayoutParams contains two values:
MATCH_PARENT: The child element must be as big as the parent element (minus [padding]).
WRAP_CONTENT: child elements only need to be able to place their content (with padding)
Different subclasses of ViewGroup have different LayoutParams. For example, RelativeLayout also has its own LayoutParams class.
This allows RelativeLayout to place child elements in the center horizontally and vertically.
The parent element can use the MeasureSpec class to limit the size of child elements. The value of MeasureSpec can be one of the following three.
UNSPECIFIED: It is used only to obtain the value of the child element request and is not limited to the child element. For example, LinearLayout can set the length and width of its child elements to UNSPECIFIED and EXACTLY to 240 respectively, so that we can know that when the length of the child element is 240px, what is the required height.
EXACTLY: the parent element limits the value of the child element to the specified value. The child element must use this value and ensure that all its child elements are available within this size.
AT_MOST: parent element refers to the maximum size of the child element. The child element must ensure that it and all its descendant elements are available in this size.
Note: The Android framework does not draw a View that is not in an invalid region, and is responsible for drawing the View background.
We can force a View to be redrawn by calling the invalidate () method.
Call requestLayout () to initialize the layout. This method is called when a View object deems that the current region cannot work normally.
About valid and invalid regions: Invalid regions need to be updated, and valid regions do not need to be updated.
Anyone with windows development experience should know that when a window is blocked to appear, the window will be drawn again, and the blocked area is invalid. Because when this area appears again, we must ensure that its content is up-to-date, so it needs to be updated, that is, re-drawn.
A valid region is called a valid region because it is in the latest status and does not need to be updated. Therefore, it does not need to be repainted.