how-android-draws.jd revision 3b3145ed6c23d99ccf6ab559552399148a5a59c9
1page.title=How Android Draws Views
2parent.title=User Interface
3parent.link=index.html
4@jd:body
5
6
7<p>When an Activity receives focus, it will be requested to draw its layout.
8The Android framework will handle the procedure for drawing, but the Activity must provide
9the root node of its layout hierarchy.</p>
10
11<p>Drawing begins with the root node of the layout. It is requested to measure and 
12draw the layout tree. Drawing is handled by walking the tree and rendering each View that
13   intersects the invalid region. In turn, each View group is responsible for requesting
14each of its children to be drawn (with the <code>{@link android.view.View#draw(Canvas) draw()}</code> method) 
15and each View is responsible for drawing itself.
16 Because the tree is traversed in-order,
17   this means that parents will be drawn before (i.e., behind) their children, with
18   siblings drawn in the order they appear in the tree.
19   </p>
20
21<div class="sidebox-wrapper">
22<div class="sidebox">
23  <p>The framework will not draw Views that are not in the invalid region, and also 
24   will take care of drawing the Views background for you.</p>
25   <p>You can force a View to draw, by calling <code>{@link android.view.View#invalidate()}</code>.
26   </p>
27</div>
28</div>
29
30<p>
31   Drawing the layout is a two pass process: a measure pass and a layout pass. The measuring
32   pass is implemented in <code>{@link android.view.View#measure(int, int)}</code> and is a top-down traversal
33   of the View tree. Each View pushes dimension specifications down the tree
34   during the recursion. At the end of the measure pass, every View has stored
35   its measurements. The second pass happens in
36   <code>{@link android.view.View#layout(int,int,int,int)}</code> and is also top-down. During
37   this pass each parent is responsible for positioning all of its children
38   using the sizes computed in the measure pass.
39   </p>
40   
41   <p>
42   When a View's <code>measure()</code> method returns, its <code>{@link android.view.View#getMeasuredWidth()}</code> and
43   <code>{@link android.view.View#getMeasuredHeight()}</code> values must be set, along with those for all of
44   that View's descendants. A View's measured width and measured height values
45   must respect the constraints imposed by the View's parents. This guarantees
46   that at the end of the measure pass, all parents accept all of their
47   children's measurements. A parent View may call <code>measure()</code> more than once on
48   its children. For example, the parent may measure each child once with
49   unspecified dimensions to find out how big they want to be, then call
50   <code>measure()</code> on them again with actual numbers if the sum of all the children's
51   unconstrained sizes is too big or too small (i.e., if the children don't agree among themselves
52  as to how much space they each get, the parent will intervene and set the rules on the second pass).
53   </p>
54   
55<div class="sidebox-wrapper">
56<div class="sidebox"><p>
57   To initiate a layout, call <code>{@link android.view.View#requestLayout}</code>. This method is typically
58   called by a View on itself when it believes that is can no longer fit within
59   its current bounds.</p>
60</div>
61</div>
62
63   <p>
64   The measure pass uses two classes to communicate dimensions. The
65   {@link android.view.View.MeasureSpec} class is used by Views to tell their parents how they
66   want to be measured and positioned. The base LayoutParams class just
67   describes how big the View wants to be for both width and height. For each
68   dimension, it can specify one of:</p>
69   <ul>
70    <li> an exact number
71    <li><var>FILL_PARENT</var>, which means the View wants to be as big as its parent
72    (minus padding)</li>
73    <li><var>WRAP_CONTENT</var>, which means that the View wants to be just big enough to
74    enclose its content (plus padding).</li>
75   </ul>
76  <p>There are subclasses of LayoutParams for different subclasses of ViewGroup.
77   For example, RelativeLayout has its own subclass of LayoutParams, which includes
78   the ability to center child Views horizontally and vertically.
79   </p>
80   
81   <p>
82   MeasureSpecs are used to push requirements down the tree from parent to
83   child. A MeasureSpec can be in one of three modes:</p>
84   <ul>
85    <li><var>UNSPECIFIED</var>: This is used by a parent to determine the desired dimension
86    of a child View. For example, a LinearLayout may call <code>measure()</code> on its child
87    with the height set to <var>UNSPECIFIED</var> and a width of <var>EXACTLY</var> 240 to find out how
88    tall the child View wants to be given a width of 240 pixels.</li>
89    <li><var>EXACTLY</var>: This is used by the parent to impose an exact size on the
90    child. The child must use this size, and guarantee that all of its
91    descendants will fit within this size.</li>
92    <li><var>AT_MOST</var>: This is used by the parent to impose a maximum size on the
93    child. The child must guarantee that it and all of its descendants will fit
94    within this size.</li>
95   </ul>
96   
97
98
99