When an activity gets the focus, it is asked to draw its layout. The Android framework will handle the process of drawing, but the activity must provide the root node of its layout system. Drawing will start from the root node, and the root node is required to measure and draw the layout tree. Drawing is done by traversing the tree and rendering each view within the active area. Each view group is in turn responsible for requiring each of its child elements to draw itself, and each view is responsible for drawing itself. Since the tree uses the middle order traversal (In-order traverse), this means that the parent element is drawn first, and then the child elements are drawn in the order in which they appear. (Translator Note: Here In-order is suspected to be pre-order)
The Android framework does not draw a view in an invalid area and will help you draw a background view.
You can call the invalidate () method to force a view to redraw.
Drawing a layout is a two-time process: a measurement process and a layout process. The measurement process is implemented in measure (Int,int), which is a top-down traversal of the view tree. Each view passes the size requirement down one level. At the end of the measurement process, each view stores its measured values. The second process takes place in Layour (Int,int,int,int), also from top to bottom. In this process, each parent element is responsible for placing the child element in the appropriate position based on the size calculated during the measurement.
When a view's measure () method returns, the Getmeasuredwidth () and getmeasuredheight () values of its descendant elements must be set. The measurement width and measurement height of a view must follow the limits of its parent view. This guarantees that after the measurement process, all parent elements accept the measured values of their child elements. A parent view may call measure () more than once on its child elements. For example, a parent element might measure each child element without parameters one at a time to get the size they want, and then call the measure () with the parameter again if the size of the child element exceeds the limit. (That is, if child elements are not reconciled to each other, the parent element sets a rule for them.)
Use Requestlayout () to initialize a layout. This method is typically called by a view to itself when it thinks it cannot be placed in its current restricted area.
The measurement process uses two classes to pass the dimensions. View uses View.measurespec to tell their parent elements how they want to be measured and placed. The Layoutparams class describes how much a view expects its length and width. It can specify one of the following:
- A definite value.
- Indicates that the view wants to be as large as its parent (except padding)
- Indicates that the view wants to include its content (plus padding)
Different subclasses of the layoutparams can be used for different viewgroup subclasses. For example, Relativelayout has its own layoutparams subclass, which includes the ability to center each sub-view horizontally and vertically.
Measurespecs can be used to pass requirements down. One measurespec can be one of three modes:
- UNSPECIFIED: A parent element uses it to determine the desired size of a child view. For example, a linearlayout may need to call measure () on its child elements, set the height to unspecified, and set the width to exactly 240来 to get the desired height of the child view at 240 pixels in width.
- Exactly: A parent element that uses it to impose an exact size for child elements. The child element must use this size and ensure that all descendant elements of it adhere to this restriction.
- At_most: The parent element uses it to impose a maximum size for child elements. The child element must ensure that it and all its descendants are within this size
Original link: http://blog.csdn.net/liao3841054/article/details/6980546
How Android is drawing the view