OSDN Git Service

d2c42d52a5b17394ada8152ab814ad8d30b2c3b8
[android-x86/frameworks-base.git] / docs / html / guide / developing / debugging / debugging-ui.jd
1 page.title=Debugging and Profiling User Interfaces
2 parent.title=Debugging
3 parent.link=index.html
4 @jd:body
5
6  <div id="qv-wrapper">
7     <div id="qv">
8       <h2>In this document</h2>
9
10       <ol>
11         <li>
12             <a href="#HierarchyViewer">
13                 Debugging and Optimizing User Interfaces with Hierarchy Viewer
14             </a>
15             <ol>
16                 <li><a href="#runhv">Running Hierarchy Viewer and choosing a window</a></li>
17                 <li><a href="#viewhierarchy">About the View Hierarchy window</a></li>
18                 <li><a href="#indiView">Working with an individual View in Tree View</a></li>
19                 <li><a href="#hvdebugging">Debugging with View Hierarchy</a></li>
20                 <li><a href="#hvoptimize">Optimizing with View Hierarchy</a></li>
21             </ol>
22         </li>
23         <li>
24             <a href="#pixelperfect">
25                 Examining and Designing User Interfaces with Pixel Perfect
26             </a>
27             <ol>
28                 <li><a href="#aboutpixelperfect">About the Pixel Perfect window</a></li>
29                 <li><a href="#overlays">Working with Pixel Perfect overlays</a></li>
30             </ol>
31         </li>
32         <li><a href="#layoutopt">Optimizing Layouts with <code>layoutopt</code></a></li>
33       </ol>
34     </div>
35   </div>
36
37   <p>Sometimes your application's layout can slow down your application.
38   To help debug issues in your layout, the Android SDK provides the Hierarchy Viewer and
39   <code>layoutopt</code> tools.
40   </p>
41
42   <p>The Hierarchy Viewer application allows you to debug and optimize your user interface. It
43   provides a visual representation of the layout's View hierarchy (the View Hierarchy window)
44   and a magnified view of the display (the Pixel Perfect window).</p>
45
46   <p><code>layoutopt</code> is a command-line tool that helps you optimize the layouts and layout
47   hierarchies of your applications. You can run it against your layout files or resource
48   directories to quickly check for inefficiencies or other types of problems that could be
49   affecting the performance of your application.</p>
50
51 <h2 id="HierarchyViewer">Debugging and Optimizing User Interfaces with Hierarchy Viewer</h2>
52
53 <h3 id="runhv">Running Hierarchy Viewer and choosing a window</h3>
54 <p>
55     To run Hierarchy Viewer, follow these steps:</p>
56 <ol>
57     <li>
58         Connect your device or launch an emulator.
59         <p>
60             To preserve security, Hierarchy Viewer can only connect to devices running a
61             developer version of the Android system.
62         </p>
63     </li>
64     <li>
65         If you have not done so already, install the application you want to work with.
66     </li>
67     <li>
68         Run the application, and ensure that its UI is visible.
69     </li>
70     <li>
71         From a terminal, launch <code>hierarchyviewer</code> from the
72         <code>&lt;sdk&gt;/tools/</code>
73         directory.
74     </li>
75     <li>
76         The first window you see displays a list of devices and emulators. To expand the list
77         of Activity objects for a device or emulator, click the arrow on the left. This displays a
78         list of the Activity objects whose UI is currently visible on the device or emulator. The
79         objects are listed by their Android component name. The list includes both your application
80         Activity and system Activity objects. A screenshot of this window appears in
81         figure 1.
82     </li>
83     <li>
84         Select the name of your Activity from the list. You can now look at its view
85         hierarchy using the View Hierarchy window, or look at a magnified image of the UI using
86         the Pixel Perfect window.
87     </li>
88 </ol>
89 <p>
90     To learn how to use the View Hierarchy window, go to
91     <a href="#viewhierarchy">About the View Hierarchy window</a>. To learn how to use the
92     Pixel Perfect window, go to <a href="#pixelperfect">About the Pixel Perfect window</a>.
93 </p>
94 <img id="Fig1" src="{@docRoot}images/developing/hv_device_window.png" alt="" height="600"/>
95 <p class="img-caption"><strong>Figure 1.</strong> Hierarchy Viewer device window</p>
96 <h3 id="viewhierarchy">About the View Hierarchy window</h3>
97 <p>
98     The View Hierarchy window displays the View objects that form the UI of the
99     Activity that is running on your device or emulator. You use it to look at individual
100     View objects within the context of the entire View tree. For each View object, the View
101     Hierarchy window also displays rendering performance data.
102 </p>
103 <p>
104     To see the View Hierarchy window, run Hierarchy Viewer as described in
105     the section <a href="#runhv">Running Hierarchy Viewer and choosing a window</a>. Next, click
106     <strong>View Hierarchy</strong> at the top of the device window.
107 </p>
108 <p>
109     You should see four panes:
110 </p>
111 <ul>
112     <li>
113         <strong>Tree View</strong>: The left-hand pane displays the Tree View,
114         a diagram of the Activity object's hierarchy of views. Use Tree View to examine individual
115         View objects and see the relationships between View objects in your UI.
116         <p>
117             To zoom in on the pane, use the slider at the bottom of the pane, or use your mouse
118             scroll wheel. To move around in the pane or reveal View objects that are not currently
119             visible, click and drag the pane.
120         </p>
121         <p>
122             To highlight the nodes in the tree whose class or ID match a search string, enter the
123             string in the <strong>Filter by class or id:</strong> edit box at the bottom of the
124             window. The background of nodes that match the search string will change from gray to
125             bright blue.
126         </p>
127         <p>
128             To save a screenshot of Tree View to a PNG file, click <strong>Save As PNG</strong> at
129             the top of the View Hierarchy window. This displays a dialog in which you can choose
130             a directory and file name.
131         </p>
132         <p>
133             To save a layered screenshot of your device or emulator to an Adobe Photoshop (PSD)
134             file, click <strong>Capture Layers</strong> at the top of the View Hierarchy window.
135             This displays a dialog in which you can choose a directory or file name.
136             Each View in the UI is saved as a separate Photoshop layer.
137         </p>
138         <p>
139             In Photoshop (or similar program that accepts .psd files), you can hide, show or edit a
140             layer independently of others. When you save a layered screenshot, you can examine and
141             modify the image of an individual View object. This helps you experiment with design
142             changes.
143         </p>
144     </li>
145     <li>
146         The upper right-hand pane displays the <strong>Tree Overview</strong>, a smaller map
147         representation of the entire Tree View window. Use Tree Overview to identify the part of the
148         view tree that is being displayed in Tree View.
149         <p>
150             You can also use Tree Overview to move around in the Tree View pane. Click and drag
151             the shaded rectangle over an area to reveal it in Tree View.
152         </p>
153     </li>
154     <li>
155         The middle right-hand pane displays the <strong>Properties View</strong>,
156         a list of the properties for a selected View object. With Properties View, you can
157         examine all the properties without having to look at your application source.
158         <p>
159             The properties are organized by category. To find an individual property, expand
160             a category name by clicking the arrow on its left. This reveals all the properties
161             in that category.
162         </p>
163     </li>
164     <li>
165         The lower right-hand pane displays the <strong>Layout View</strong>,
166         a block representation of the UI. Layout View is another way to navigate through your UI.
167         When you click on a View object in Tree View, its position in the UI is highlighted.
168         Conversely, when you click in an area of Layout View, the View object for that area is
169         highlighted in Tree View.
170         <p>
171             The outline colors of blocks in Layout View provide additional information:
172         </p>
173             <ul>
174                 <li>
175                     Bold red: The block represents the the View that is currently selected in
176                     Tree View.
177                 </li>
178                 <li>
179                     Light red: The block represents the parent of the block outlined in bold red.
180                 </li>
181                 <li>
182                     White: The block represents a visible View that is not a parent or child of the
183                     View that is currently selected in Tree View.
184                 </li>
185             </ul>
186     </li>
187 </ul>
188 <p>
189     When the UI of the current Activity changes, the View Hierarchy window is not automatically
190     updated. To update it, click <strong>Load View Hierarchy</strong> at the top of the window.
191 </p>
192 <p>
193     Also, the window is not updated if you switch to a new Activity. To update it, start by
194     clicking the window selection icon in the bottom left-hand corner of the window. This
195     navigates back to the Window Selection window. From this window, click the Android
196     component name of the new Activity and then click <strong>Load View Hierarchy</strong>
197     at the top of the window.
198 </p>
199 <p>
200     A screenshot of the View Hierarchy window appears in figure 2.
201 </p>
202 <img id="Fig2" src="{@docRoot}images/developing/hv_view_hierarchy_window.png" alt="" height="600"/>
203 <p class="img-caption"><strong>Figure 2.</strong> The View Hierarchy window</p>
204 <h3 id="indiView">Working with an individual View in Tree View</h3>
205 <p>
206     Each node in Tree View represents a single View. Some information is always visible. Starting
207     at the top of the node, you see the following:
208 </p>
209 <ol>
210     <li>
211         View class: The View object's class.
212     </li>
213     <li>
214         View object address: A pointer to View object.
215     </li>
216     <li>
217         View object ID: The value of the
218         <code><a href="{@docRoot}guide/topics/resources/layout-resource.html#idvalue">android:id</a>
219         </code> attribute.
220     </li>
221     <li>
222         Performance indicators: A set of three colored dots that indicate the rendering
223         speed of this View relative to other View objects in the tree. The three dots
224         represent (from left to right) the measure, layout, and draw times of the rendering.
225         <p>
226             The colors indicate the following relative performance:
227         </p>
228         <ul>
229             <li>
230                 Green: For this part of the render time, this View is in the faster 50% of all
231                 the View objects in the tree. For example, a green dot for the measure time means
232                 that this View has a faster measure time than 50% of the View objects in the tree.
233             </li>
234             <li>
235                 Yellow: For this part of the render time, this View is in the slower 50% of all
236                 the View objects in the tree. For example, a yellow dot for the layout time means
237                 that this View has a slower layout time than 50% of the View objects in the tree.
238             </li>
239             <li>
240                 Red: For this part of the render time, this View is the slowest one in the tree.
241                 For example, a red dot for the draw time means that this View takes the most
242                 time to draw of all the View objects in the tree.
243             </li>
244         </ul>
245     </li>
246     <li>
247         View index: The zero-based index of the View in its parent View. If it is the only child,
248         this is 0.
249     </li>
250 </ol>
251 <p>
252     When you select a node, additional information for the View appears in a small window above
253     the node. When you click one of the nodes, you see the following:
254 </p>
255 <ul>
256     <li>
257         Image: The actual image of the View, as it would appear in the emulator. If the View has
258         children, these are also displayed.
259     </li>
260     <li>
261         View count: The number of View objects represented by this node. This includes the View
262         itself and a count of its children. For example, this value is 4 for a View that has 3
263         children.
264     </li>
265     <li>
266         Render times: The actual measure, layout, and draw times for the View rendering, in
267         milliseconds. These represent the same values as the performance indicators mentioned in
268         the preceding section.
269     </li>
270 </ul>
271 <p>
272     An annotated screenshot of an individual node in the Tree View window appears in figure 3.
273 </p>
274 <img id="Fig3" src="{@docRoot}images/developing/hv_treeview_screenshot.png" alt="" height="600"/>
275 <p class="img-caption"><strong>Figure 3.</strong> An annotated node in Tree View</p>
276 <h3 id="hvdebugging">Debugging with View Hierarchy</h3>
277 <p>
278     The View Hierarchy window helps you debug an application by providing a static display
279     of the UI. The display starts with your application's opening screen. As you step through
280     your application, the display remains unchanged until you redraw it by invalidating and
281     then requesting layout for a View.
282 </p>
283 <p>
284     To redraw a View in the display:
285 </p>
286     <ul>
287         <li>
288             Select a View in Tree View. As you move up towards the root of the tree (to the
289             left in the Tree View), you see the highest-level View objects. Redrawing a high-level
290             object usually forces the lower-level objects to redraw as well.
291         </li>
292         <li>
293             Click <strong>Invalidate</strong> at the top of the window. This marks the View as
294             invalid, and schedules it for a redraw at the next point that a layout is requested.
295         </li>
296         <li>
297             Click <strong>Request Layout</strong> to request a layout. The View and its children
298             are redrawn, as well as any other View objects that need to be redrawn.
299         </li>
300     </ul>
301 <p>
302     Manually redrawing a View allows you to watch the View object tree and examine the properties of
303     individual View objects one step at a time as you go through breakpoints in your code.
304 </p>
305 <h3 id="hvoptimize">Optimizing with View Hierarchy</h3>
306 <p>
307     View Hierarchy also helps you identify slow render performance. You start by looking at the
308     View nodes with red or yellow performance indicators to identify the slower View objects. As you
309     step through your application, you can judge if a View is consistently slow or slow only in
310     certain circumstances.
311 </p>
312 <p>
313     Remember that slow performance is not necessarily evidence of a problem, especially for
314     ViewGroup objects. View objects that have more children and more complex View objects render
315     more slowly.
316 </p>
317 <p>
318     The View Hierarchy window also helps you find performance issues. Just by looking at the
319     performance indicators (the dots) for each View node, you can see which View objects are the
320     slowest to measure, layout, and draw. From that, you can quickly identify the problems you
321     should look at first.
322 </p>
323 <h2 id="pixelperfect">Examining and Designing User Interfaces with Pixel Perfect</h2>
324 <p>
325     Pixel Perfect is a tool for examining pixel properties and laying out UIs from a design drawing.
326 </p>
327 <h3 id="aboutpixelperfect">About the Pixel Perfect window</h3>
328 <p>
329     The Pixel Perfect window displays a magnified image of the screen that is currently
330     visible on the emulator or device. In it, you can examine the properties
331     of individual pixels in the screen image. You can also use the Pixel Perfect window
332     to help you lay out your application UI based on a bitmap design.
333 </p>
334 <p>
335     To see the Pixel Perfect window, run Hierarchy Viewer, as described in
336     the section <a href="#runhv">Running Hierarchy Viewer and choosing a window</a>. Next, click
337     <strong>Inspect Screenshot</strong> at the top of the device window. The Pixel Perfect window
338     appears.
339 </p>
340 <p>
341     In it, you see three panes:
342 </p>
343 <ul>
344     <li>
345         View Object pane: This is a hierarchical list of the View objects that are currently
346         visible on the device or emulator screen, including both the ones in your application and
347         the ones generated by the system. The objects are listed by their View class.
348         To see the class names of a View object's children, expand the View by clicking the
349         arrow to its left. When you click a View, its position is highlighted in the Pixel Perfect
350         pane on the right.
351     </li>
352     <li>
353         Pixel Perfect Loupe pane: This is the magnified screen image. It is overlaid by a grid in
354         which each square represents one pixel. To look at the information for a pixel, click in its
355         square. Its color and X,Y coordinates appear at the bottom of the pane.
356         <p>
357             The magenta crosshair in the pane corresponds to the positioning
358             crosshair in the next pane. It only moves when you move the crosshair in the next pane.
359         </p>
360         <p>
361             To zoom in or out on the image, use the <strong>Zoom</strong> slider at the bottom of
362             the pane, or use your mouse's scroll wheel.
363         </p>
364         <p>
365             When you select a pixel in the Loupe pane, you see the following information at the
366             bottom of the pane:
367         </p>
368         <ul>
369             <li>
370                 Pixel swatch: A rectangle filled with the same color as the pixel.
371             </li>
372             <li>
373                 HTML color code: The hexadecimal RGB code corresponding to the pixel color
374             </li>
375             <li>
376                 RGB color values: A list of the (R), green (G), and blue (B) color values of the
377                 pixel color. Each value is in the range 0-255.
378             </li>
379             <li>
380                 X and Y coordinates: The pixel's coordinates, in device-specific pixel units.
381                 The values are 0-based, with X=0 at the left of the screen and Y=0 at the top.
382             </li>
383         </ul>
384     </li>
385     <li>
386         Pixel Perfect pane: This displays the currently visible screen as it would appear in the
387         emulator.
388         <p>
389             You use the cyan crosshair to do coarse positioning. Drag the crosshair in the image,
390             and the Loupe crosshair will move accordingly. You can also click on a point in the
391             Pixel Perfect pane, and the crosshair will move to that point.
392         </p>
393         <p>
394             The image corresponding to the View object selected in the View Object pane is
395             outlined in a box that indicates the View object's position on the screen. For the
396             selected object, the box is bold red. Sibling and parent View objects have a light
397             red box. View objects that are neither parents nor siblings are in white.
398         </p>
399         <p>
400             The layout box may have other rectangles either inside or outside it, each of which
401             indicates part of the View. A purple or green rectangle indicates the View bounding box.
402             A white or black box inside the layout box represents the <strong>padding</strong>, the
403             defined distance between the View object's content and its bounding box. An outer white
404             or black rectangle represents the <strong>margins</strong>, the distance between the
405             View bounding box and adjacent View objects. The padding and margin boxes are white if
406             the layout background is black, and vice versa.
407         </p>
408         <p>
409             You can save the screen image being displayed in the Pixel Perfect pane as a PNG file.
410             This produces a screenshot of the current screen. To do this, click
411             <strong>Save as PNG</strong> at the top of the window. This displays a dialog,
412             in which you can choose a directory and filename for the file.
413         </p>
414     </li>
415 </ul>
416 <p>
417     The panes are not automatically refreshed when you change one of the View objects or go to
418     another Activity. To refresh the Pixel Perfect pane and the Loupe pane, click
419     <strong>Refresh Screenshot</strong> at the top of the window. This will change the panes
420     to reflect the current screen image. You still may need to refresh the View Object pane;
421     to do this, click <strong>Refresh Tree</strong> at the top of the window.
422 </p>
423 <p>
424     To automatically refresh the panes while you are debugging, set
425     <strong>Auto Refresh</strong> at the top of the window, and then set a refresh rate
426     with the <strong>Refresh Rate</strong> slider at the bottom of the Loupe pane.
427 </p>
428 <h3 id="overlays">Working with Pixel Perfect overlays</h3>
429 <p>
430     You often construct a UI based on a design done as a bitmap image. The Pixel Perfect window
431     helps you match up your View layout to a bitmap image by allowing you to load the bitmap as an
432     <strong>overlay</strong> on the screen image.
433 </p>
434 <p>
435     To use a bitmap image as an overlay:
436 </p>
437 <ul>
438     <li>
439         Start your application in a device or emulator and navigate to the Activity whose UI you
440         want to work with.
441     </li>
442     <li>
443         Start Hierarchy Viewer and navigate to the Pixel Perfect window.
444     </li>
445     <li>
446         At the top of the window, click <strong>Load Overlay</strong>. A dialog opens, prompting
447         for the image file to load. Load the image file.
448     </li>
449     <li>
450         Pixel Perfect displays the overlay over the screen image in the Pixel Perfect pane. The
451         lower left corner of the bitmap image (X=0, Y=<em>max value</em>) is anchored on the lower
452         leftmost pixel (X=0, Y=<em>max screen</em>) of the screen.
453         <p>
454             By default, the overlay has a 50% transparency, which allows you to see the screen
455             image underneath. You can adjust this with the <strong>Overlay:</strong> slider at the
456             bottom of the Loupe pane.
457         </p>
458         <p>
459             Also by default, the overlay is not displayed in the Loupe pane. To display it,
460             set <strong>Show in Loupe</strong> at the top of the window.
461         </p>
462     </li>
463 </ul>
464 <p>
465     The overlay is not saved as part of the screenshot when you save the screen image as a PNG
466     file.
467 </p>
468 <p>
469     A screenshot of the Pixel Perfect window appears in figure 4.
470 </p>
471 <img id="Fig4" src="{@docRoot}images/developing/hv_pixelperfect.png"
472         alt=""
473         height="600"/>
474 <p class="img-caption"><strong>Figure 4.</strong> The Pixel Perfect window</p>
475 <h2 id="layoutopt">Optimizing layouts with layoutopt</h2>
476 <p>
477     The <code>layoutopt</code> tool lets you analyze the XML files that define your
478     application's UI to find inefficiencies in the view hierarchy.</p>
479
480 <p>
481     To run the tool, open a terminal and launch <code>layoutopt &lt;xmlfiles&gt;</code>
482     from your SDK <code>tools/</code> directory. The &lt;xmlfiles&gt; argument is a space-
483     delimited list of resources you want to analyze, either uncompiled resource xml files or
484     directories of such files.
485 </p>
486 <p>
487     The tool loads the specified XML files and analyzes their definitions and
488     hierarchies according to a set of predefined rules. For every issue it detects, it
489     displays the following information:
490 </p>
491 <ul>
492     <li>
493         The filename in which the issue was detected.
494     </li>
495     <li>
496         The line number for the issue.
497     </li>
498     <li>
499         A description of the issue, and for some types of issues it also suggests a resolution.
500     </li>
501 </ul>
502 <p>The following is a sample of the output from the tool:</p>
503 <pre>
504 $ layoutopt samples/
505 samples/compound.xml
506    7:23 The root-level &lt;FrameLayout/&gt; can be replaced with &lt;merge/&gt;
507    11:21 This LinearLayout layout or its FrameLayout parent is useless
508 samples/simple.xml
509    7:7 The root-level &lt;FrameLayout/&gt; can be replaced with &lt;merge/&gt;
510 samples/too_deep.xml
511    -1:-1 This layout has too many nested layouts: 13 levels, it should have &lt;= 10!
512    20:81 This LinearLayout layout or its LinearLayout parent is useless
513    24:79 This LinearLayout layout or its LinearLayout parent is useless
514    28:77 This LinearLayout layout or its LinearLayout parent is useless
515    32:75 This LinearLayout layout or its LinearLayout parent is useless
516    36:73 This LinearLayout layout or its LinearLayout parent is useless
517    40:71 This LinearLayout layout or its LinearLayout parent is useless
518    44:69 This LinearLayout layout or its LinearLayout parent is useless
519    48:67 This LinearLayout layout or its LinearLayout parent is useless
520    52:65 This LinearLayout layout or its LinearLayout parent is useless
521    56:63 This LinearLayout layout or its LinearLayout parent is useless
522 samples/too_many.xml
523    7:413 The root-level &lt;FrameLayout/&gt; can be replaced with &lt;merge/&gt;
524    -1:-1 This layout has too many views: 81 views, it should have &lt;= 80!
525 samples/useless.xml
526    7:19 The root-level &lt;FrameLayout/&gt; can be replaced with &lt;merge/&gt;
527    11:17 This LinearLayout layout or its FrameLayout parent is useless
528 </pre>