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