| page.title=How Android Draws Views |
| parent.title=Views and Layout |
| parent.link=index.html |
| @jd:body |
| |
| |
| <p>As mentioned in the introduction to <a href="index.html">Views and Layout</a>, |
| drawing begins when the Activity requests the root node of the layout to measure and |
| draw the layout tree. Drawing is handled by walking the tree and rendering each view that |
| intersects the invalid region. In turn, each view group is responsible for requesting |
| each of its children to be drawn and each view is responsible for drawing itself. |
| Because the tree is traversed in-order, |
| this means that parents will be drawn before (i.e., behind) their children, with |
| siblings drawn in the order they appear in the tree. |
| </p> |
| |
| <div class="sidebox"> |
| <p>The framework will not draw views that are not in the invalid region, and also |
| will take care of drawing the views background for you.</p> |
| <p>You can force a view to draw, by calling {@link android.view.View#invalidate()}. |
| </p> |
| </div> |
| |
| <p> |
| Drawing the layout is a two pass process: a measure pass and a layout pass. The measuring |
| pass is implemented in {@link android.view.View#measure(int, int)} and is a top-down traversal |
| of the view tree. Each view pushes dimension specifications down the tree |
| during the recursion. At the end of the measure pass, every view has stored |
| its measurements. The second pass happens in |
| {@link android.view.View#layout(int,int,int,int)} and is also top-down. During |
| this pass each parent is responsible for positioning all of its children |
| using the sizes computed in the measure pass. |
| </p> |
| |
| <p> |
| When a view's measure() method returns, its {@link android.view.View#getMeasuredWidth()} and |
| {@link android.view.View#getMeasuredHeight()} values must be set, along with those for all of |
| that view's descendants. A view's measured width and measured height values |
| must respect the constraints imposed by the view's parents. This guarantees |
| that at the end of the measure pass, all parents accept all of their |
| children's measurements. A parent view may call measure() more than once on |
| its children. For example, the parent may measure each child once with |
| unspecified dimensions to find out how big they want to be, then call |
| measure() on them again with actual numbers if the sum of all the children's |
| unconstrained sizes is too big or too small (i.e., if the children don't agree among themselves |
| as to how much space they each get, the parent will intervene and set the rules on the second pass). |
| </p> |
| |
| <div class="sidebox"><p> |
| To intiate a layout, call {@link android.view.View#requestLayout}. This method is typically |
| called by a view on itself when it believes that is can no longer fit within |
| its current bounds.</p> |
| </div> |
| |
| <p> |
| The measure pass uses two classes to communicate dimensions. The |
| {@link android.view.View.MeasureSpec} class is used by views to tell their parents how they |
| want to be measured and positioned. The base LayoutParams class just |
| describes how big the view wants to be for both width and height. For each |
| dimension, it can specify one of:</p> |
| <ul> |
| <li> an exact number |
| <li>FILL_PARENT, which means the view wants to be as big as its parent |
| (minus padding)</li> |
| <li> WRAP_CONTENT, which means that the view wants to be just big enough to |
| enclose its content (plus padding).</li> |
| </ul> |
| <p>There are subclasses of LayoutParams for different subclasses of ViewGroup. |
| For example, AbsoluteLayout has its own subclass of LayoutParams which adds |
| an X and Y value. |
| </p> |
| |
| <p> |
| MeasureSpecs are used to push requirements down the tree from parent to |
| child. A MeasureSpec can be in one of three modes:</p> |
| <ul> |
| <li>UNSPECIFIED: This is used by a parent to determine the desired dimension |
| of a child view. For example, a LinearLayout may call measure() on its child |
| with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how |
| tall the child view wants to be given a width of 240 pixels.</li> |
| <li>EXACTLY: This is used by the parent to impose an exact size on the |
| child. The child must use this size, and guarantee that all of its |
| descendants will fit within this size.</li> |
| <li>AT_MOST: This is used by the parent to impose a maximum size on the |
| child. The child must gurantee that it and all of its descendants will fit |
| within this size.</li> |
| </ul> |
| |
| |
| |