Optimizing Your ui--using Hierarchy Viewer to optimize UI

Source: Internet
Author: User
Tags list of attributes

Sometimes the layout of your application may make your app run slowly. To help debug problems in the layout, the Android SDK provides the Hierachy Viewer and lint tools.

The Hierarchy Viewer app allows you to debug and optimize your UI. It provides a visual representation of the layout view hierarchy (view Hierachy window), with performance information for each node in the layout, and a magnified display view (Pixel perfect window) to examine the layout's pixels more closely.

The Android Lint tool is a static code scanning tool that helps you optimize your application's layout and layout levels while discovering other common coding issues. You can run it on a layout file or resource directory to quickly check for low performance or other issues that affect the type of application energy.

Use the Hierarchy Viewer to run the Hierarchy Viewer and select a window

Follow these steps to run the Hierachy Viewer:

    1. Connect your device or run an emulator
      To protect security, the Hierarchy Viewer can only connect devices running the developer version of the Android system.
    2. Install the apps you want to work with
    3. Run the app to make sure that its UI is visible.
    4. At the terminal, run the hierachyviewer of the <sdk>/tools/ directory.
    5. The first window you see shows a list of devices and emulators. To expand the list of activity objects for a device or emulator, click the arrow on the left. This shows a list of activity objects currently visible to the UI on the device or emulator. These objects are listed according to their Android component names. This list contains the activity objects that you apply and the activity objects of the system.
    6. Select your activity name from the list. You can now view its view hierarchy using the View Hierarchy window, or use the pixel perfect window to view a magnified image of the UI.


Note: Hierarchy Viewer Device window

About the View Hierarchy window

The View Hierarchy window shows the View object that runs the activity UI on the device or emulator. You can view each individual view object in the context of the entire view tree. For each View object, the View Hierarchy window also displays rendering performance data.

To view the View Hierarchy window, run Hierarchy Viewer in the section above and click View Hierarchyabove the device window.

You should see a four-pane:

  • tree view: The left pane shows the tree view, a graph showing the level of the activity's view object. Use the tree view to examine individual view objects and to view the relationships between view objects in the UI.

    To zoom in the pane, use the slider bar at the bottom of the pane, or use the mouse wheel. To move or display the currently invisible View object in the pane, tap and drag the pane.

    To highlight a node whose class or ID matches a search string in the tree, enter the string in the Filter by class or ID: edit box at the bottom of the window. The background of the node that matches the search string changes from gray to light blue.

    To save a snapshot of a tree view into a PNG file, click Save as PNGat the top of the View hierarchy window. This action displays a dialog box where you can select a directory and file name.

    To save your device or emulator's hierarchy snapshot to an Adobe Photoshop (PSD) file, click Capture Layersat the top of the View hierarchy window. This action displays a dialog box where you can select a directory for the name of the goods. Each view in the UI is stored as a separate Photoshop layer.

    In Photoshop (or something like a program that handles. psd files), you can hide, show, or edit a layer independently of the other. When you want to save a hierarchical snapshot, you can examine and modify the images for each individual view object. This helps you to experiment with design changes.

  • The upper-right pane shows the tree Overview, a small map representing the entire tree view window. Use the tree overview to identify which part of the view tree is displayed in the tree view.
    You can also use the tree overview to move back and forth in the tree view pane. Click and drag the shadow rectangle to cover an area to display in the tree view.

  • The right middle pane displays the Properties view, a list of attributes for the selected View object. With properties view, you can check all the attributes without looking at your app's source code.
    Properties are grouped by class. To find a separate property, click the arrow on the left to expand a category name. This action displays all the attributes of this category.

  • The lower-right pane shows Layout View, a piece of content that represents the UI. Layout view is another way to navigate the UI. When you click a View object in the tree view, its position is highlighted in the UI. Conversely, when you click an area in the layout view, the View object in the Tree view should be highlighted.
    The outline color of blocks in Layout view provides additional information:

    • Coarse Red: This block represents the view that is currently selected in the tree view
    • Light Red: This block represents the parent view of a block of coarse red outline
    • White: This block represents a visual view, but not the parent view or child view of the selected view in the current TreeView.

The current activity UI changes, and the View hierarchy window does not refresh automatically. To refresh it, click Load View Hierarchyat the top of the window.

Similarly, the window is not updated when you switch to a new activity. To update it, first click the window at the bottom left of the window to select the icon. This operation navigates back to window selection. In this window, click on the new Activity's Android component name and then click Load View Hierarchyat the top of the window.

is a snapshot of the View Hierarchy window.

Working with separate view in tree view

Each of the points in the Tree view represents a separate view. Some information is always visible. First, at the top of the node, you can see the following information:

    1. View class: Class for View object
    2. View object address: A pointer to the View object.
    3. View Object ID: The value of the adnroid:id property.
    4. Performance indicator: A set of three-color dots indicates the rendering speed of this view relative to other view objects in the tree. These three points (left to right) represent the measurement, layout, and time of the rendering.
      Color indicates the following related performance:
      • Green: This part of the rendering time, this view in the tree all view objects in the fastest 50%. For example, the measurement time of a green point means that the view is measured more quickly than the 50%view object in the tree.
      • Yellow: This part of the rendering time, this view in the tree all view objects in the slowest 50%. For example, the layout time of a yellow dot means that the layout time of the view is slower than the 50%view object in the tree.
      • Red: This part of the rendering time, this view is the slowest one in the tree. For example, a red dot's depiction time means that the view spends the most time depicting all the view objects in the tree.
    5. View index: The zero-based index in its parent view. 0 if it is the only child view.

When you select a node, the additional information for this view appears in a small window on that node. When you click on one of the nodes, you can see the following information:

    • Image: The real image of this view, which appears in the emulator. If this view has a child view, it will also be shown here.
    • View Number: This represents the number of view objects. This includes the view itself and the number of its sub-view. For example, a view with a value of 4 has 3 child view.
    • Rendering time: The actual measurement, layout, and depiction time of this view rendering, in milliseconds. These represent the same values as the performance indicators mentioned in the previous section.

Shows a comment snapshot of a separate node in the TreeView.

Debugging with View hierarchy

The View hierarchy window helps you debug your app by providing a static UI display. This display starts with your app opening screen. When you step into an app, the display remains the same until you redraw it by invalidating it and requesting the view's layout.

To redraw a view in the display:

    • Select a view in the TreeView. When you move to the root of the tree (on the left side of the trees view), you can see the top view object. Redrawing an advanced object usually forces the low-level object to also be redrawn.
    • Click Invalidateat the top of the window. This action marks the view invalidation and arranges it to redraw at the next request layout point in time.
    • Click request layout to ask for layouts. The view and its child view are redrawn, and the view objects that need to be redrawn are redrawn.

Manually redrawing a view allows you to observe the view and check the properties of individual view objects one step at a time when you enter a code breakpoint.

Using the View hierarchy optimization

The View hierarchy also helps you identify slow rendering performance. You begin to identify the slow view object by looking at the red or yellow performance indication of the view node. When you step into your app, you can tell whether a view is always slow or only in a particular environment.

Remember that slow performance is not an important proof of the problem, especially for ViewGroup objects. View objects with many sub-view and complex view objects render very slowly.

The View Hierarchy window also helps you find performance issues. Simply by looking at the performance indicators (those points) for each view node, you can see which view objects are measured, and the layout and depiction are the slowest. From here, you can quickly identify the issues you should look at first.

Using pixel Perfect

Pixel Perfect is a tool to examine pixel properties and layout ui from a design diagram.

About the pixel perfect window

Pixel Perfect shows a magnified image of the currently visible screen on the emulator or device. Here, you can check the properties of individual pixels in the screen image. You can also use the pixel perfect window to help you lay out your app UI based on a bitmap design.

To see the Pixel perfect window, run the hierarchy Viewer, then click Inspect screenshotabove the device window and the Pixel perfect window will appear. Here you see three panes of the pane:

  • View Object pane: This is a hierarchical list of view objects currently visible on the device or emulator screen, including your app's view and system-generated view. These are listed in the view class that you want to use with them. To view the class name of a view object's child view, click the arrow to the left of it to expand view. When you click on a view, it is highlighted in the location on the right side of the pixel perfect pane.

  • Pixel Perfect Loupe pane: This is the magnified screen image. It is covered by a grid and each block represents a pixel. Click on the block to view a pixel's information. Its color and x, y coordinates appear at the bottom of the pane.

    The magenta Cross in the pane corresponds to the position cross in the next pane. It only moves when you move the cross in the next pane.

    To scale an image, use the Zoom Bar at the bottom of the pane, or use the mouse wheel.

    When you select a pixel in the Loupe pane, you can see the message below the pane:

    • Pixel Sample: A rectangle filled with the same color as the pixel.
    • HTML color code: the hexadecimal RGB code corresponding to the pixel color.
    • RGB color values: A list of red (R), Green (G), and Blue (B) color values for pixel colors. The range for each value is 0-255.
      X and Y coordinates: the coordinates of the pixel, in units of specific device pixels. This value starts at 0, x=0 on the left side of the screen, y=0 at the top.
  • Pixel Perfect pane: Displays the current visual screen in the emulator.

    Use the blue-green cross to position it rough. Drag the cross in the image and the cross in the Loupe will move accordingly. You can also click the point in the Pixel perfect pane and the cross will move to that point.

    The image for the View object selected in the View Objects pane is wrapped in a box to identify the location of the View object on the screen. For the selected object, the box is dark red. The sibling and parent view objects have a light red box. Neither the father nor the brother. The View object is a white box.

    The layout box can have other rectangles inside and outside of it, each representing the part of the view. A purple or green rectangle indicates the view bounding box. A white or black box in the layout box represents padding, which is the distance between the contents of the View object and its bounding box. An outer white or black rectangle represents margins, which is the distance between the view bounding box and the adjacent View object. If the layout background is black, the padding and margin boxes are white and vice versa.

    You can save the screen image in Pixel perfect as a PNG file. This operation produces a snapshot of the current screen. To implement it, click Save as PNGat the top of the window. It displays a dialog box where you can select the directory and file name of the file.

These panes do not automatically refresh when you change the View object or enter another activity. To refresh the pixel perfect pane and Loupe pane, click Refresh screenshotat the top of the window. This action will change the pane to display the current screen image. You may need to also refresh the View Object pane and click on the refresh Tree at the top of the window to implement it.

To automatically refresh the pane when you are debugging, set Auto Refreshat the top of the window, and then set the refresh rate with the refresh rates bar at the bottom of the Loupe pane.

Overwrite with Pixel perfect

You often construct the UI based on a bitmap image that you design. The Pixel perfect window helps you match a bitmap image to the view layout by allowing you to load the bitmap as a screen image overlay .

To use a bitmap image as an overlay step:

    • Start your app on the device or simulator and navigate to the activity you want to manipulate the UI.
    • Start the Hierarchy Viewer and navigate to the pixel perfect window.
    • At the top of the window, click load Overlayand a dialog box will open prompting you to load the image file. Then load the image file.
    • The Pixel perfect display is overlaid on the screen image of the Pixel perfect pane. The lower-left corner of the bitmap image (X=0, Y=max value) is anchored to the most seated pixel (x=0, Y=max screen) of the display.

      By default, the overlay has 50% transparency, allowing you to see the screen image below. You can use the Overlay: bar at the bottom of the Loupe pane to adjust the transparency.

      At the same time default, overrides are not displayed in the Loupe pane. Set the show in Loupe at the top of the window to display.

When you save the screen image as a PNG file, the overlay is not saved as part of the screen shot.

Displays a screenshot of the Pixel perfect window.

Use lint to optimize your UI

Android Lint lets you analyze the XML file that defines the application UI to discover the low performance of the view hierarchy.

Starting with the SDK tool version 16, the Android layoutopt tool has been replaced by the Lin tool. The Lint tool reports UI layout performance issues in a layout-like manner and finds additional issues.

Original link:
Http://developer.android.com/tools/debugging/debugging-ui.html
Android Debugging Series Tutorial:
http://blog.csdn.net/doandkeep/article/details/45173475

Optimizing Your ui--using Hierarchy Viewer to optimize UI

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.