Optimizing Your UI -- use Hierarchy Viewer to optimize the UI and hierarchyviewer

Source: Internet
Author: User

Optimizing Your UI -- use Hierarchy Viewer to optimize the UI and hierarchyviewer

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

The Hierarchy Viewer application allows you to debug and optimize the UI. It provides a visual display of the Layout View hierarchy (View Hierachy window), with the performance information of each node in the layout, and a magnified display View (Pixel Perfect window) to check the layout pixels more carefully.

The Android lint tool is a static code scanning tool that helps you optimize the layout and layout layers of applications and discover other common coding problems. You can run it on the layout file or resource directory to quickly check for low performance or other problems that may affect the application performance type.

Run Hierarchy Viewer using Hierarchy Viewer and select a window

Follow these steps to run Hierachy Viewer:


Note: Hierarchy Viewer device window

View Hierarchy window

The View Hierarchy window displays the View object of the Activity UI on the running device or simulator. 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 according to the above section, and then clickView Hierarchy.

You should see four panes:

  • Tree View: Tree View is displayed in the left pane, and a view object hierarchy chart is displayed for the Activity. Use Tree View to check individual View objects and View the relationship between View objects in the UI.

    To scale the pane, use the slider at the bottom of the pane, or use the scroll wheel. To move or display the currently invisible View object back and forth in the pane, click and drag the pane.

    To highlight the nodes in the tree where the class or ID matches a search stringFilter by class or id:Enter a string in the edit box. The background of the node that matches the search string changes from Gray to light blue.

    To save a snapshot of the Tree View to a PNG file, clickSave As PNG. This operation will display a dialog box where you can select a directory and a file name.

    To save a layered snapshot of your device or simulator to an Adobe Photoshop (PSD) file, clickCapture Layers. This operation will display a dialog box where you can select a directory file name. Each View in the UI is stored as a separate Photoshop layer.

    In Photoshop (or a program similar to processing. psd files), you can hide, display, or edit a layer, independent of others. When you want to save a hierarchical snapshot, you can check and modify the image of each individual View object. This helps you test design changes.

  • Display in the upper right cornerTree OverviewA small map that represents the entire Tree View window. Tree OverView is used to identify which part of the view tree is displayed in the Tree View.
    You can also use Tree OverView to move back and forth in the Tree View pane. Click and drag the shadow rectangle to overwrite an area to display it in the Tree View.

  • Display in the middle right paneProperties View, A list of properties of the selected View object. Through Properties View, you can check all attributes without viewing your application source code.
    Properties are grouped by class. To find a separate attribute, click the arrow on the left to show a category name. This operation will display all attributes of this category.

  • Display in the lower right paneLayout View, A piece of content to represent the UI. Layout View is another way to navigate the UI. When you click a View object in the Tree View, its position in the UI is highlighted. On the contrary, when you click an area in the Layout View, the View object in the expected area in the Tree View is highlighted.
    The outline color of a block in the Layout View provides additional information:

    • Bold red: This block indicates the View selected in the Tree View.
    • Light red: This block represents the parent View of the block with a rough red outline.
    • White: This block represents a visual View, but it is not the parent View or child View of the selected View in the current TreeView.

The UI of the current Activity is changed. The View Hierarchy window is not automatically refreshed. To refresh it, clickLoad View Hierarchy.

Similarly, the window will not be updated when you switch to a new Activity. To update it, click the window selection icon in the lower left corner of the window. This operation will navigate back to the Window Selection Window. In this window, click the Android component name of the new Activity and then clickLoad View Hierarchy.

Is a snapshot of the View Hierarchy window.

Process individual views in the Tree View

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

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

  • Image: The real image of this View, which will appear in the simulator. If this View has a subview, it will also be displayed here.
  • View number: the number of View objects. This includes the number of views and their subviews. For example, a View with this value of 4 has three sub-views.
  • Rendering time: the actual measurement, layout, and plotting time of the View rendering, in milliseconds. These represent the same value as the performance indicator mentioned above.

The comment snapshot of a single node in the TreeView is displayed.

Debug with View Hierarchy

The View Hierarchy window provides a static UI display to help you debug the application. This display starts when your application opens the screen. When you execute an application in one step, the display will remain unchanged until you repaint it by invalidating it and requesting the layout of this View.

To re-paint a View in the display, follow these steps:

  • Select a View in the TreeView. When you move to the Tree root (on the left of the Tree View), you can see the highest level View object. Repainting an advanced Object usually forces a low-level object to be repainted.
  • ClickInvalidate. This operation will mark the View as invalid and arrange it to be re-painted at the next request layout time point.
  • ClickRequest LayoutRequest layout. View and its sub-View will be re-painted, and the View objects to be re-painted will also be re-painted.

Manual re-drawing of a View allows you to observe the View and check the attributes of a single View object once when you enter the code breakpoint.

Use View Hierarchy for Optimization

View Hierarchy also helps you identify slow rendering performance. You start to identify slow View objects by checking the red or yellow performance indicators of the View node. When you execute your application in one step, you can determine whether a View is always slow or only slow in a specific environment.

Remember that slow performance is not an important evidence of the problem, especially for ViewGroup objects. Rendering of View objects and complex View objects with many sub-views is slow.

The View Hierarchy window also helps you find performance problems. By viewing the performance indicators (those points) of each View node, You can see which View objects are measured, with the slowest layout and description. From here, you can quickly identify the problems you should first check.

Use Pixel Perfect

Pixel Perfect is a tool used to check Pixel properties and layout the UI from the design drawing.

About Pixel Perfect window

Pixel Perfect displays an enlarged image of the current visual screen on the simulator 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 design and layout your application UI Based on bitmap.

To see the Pixel Perfect window, run Hierarchy Viewer and clickInspect ScreenshotThe Pixel Perfect window appears. Here, you can see three panes:

  • View Object Pane: This is a hierarchical list of View objects that are currently visible on the device or simulator screen, including the View of your application and the View generated by the system. These are listed by the View class you want to use. To View the class name of a View object sub-View, click the arrow on the left of the View object to expand the View. When you click a View, it is highlighted in the position in the Pixel Perfect pane on the right.

  • Pixel Perfect Loupe Pane: This is a magnified screen image. It is covered by a grid, and each square represents a pixel. Click the square to view the information of a pixel. Its color and X and Y coordinates appear at the bottom of the pane.

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

    To scale the image, useZoomOr use the scroll wheel.

    When you select a pixel in the Loupe pane, you can see the information 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: the red (R), Green (G), and blue (B) color values of pixel colors. Each value ranges from 0 to 255.
      X and Y coordinates: the coordinates of pixels, in the unit of pixels of a specific device. This value starts from 0, X = 0 on the left side of the screen, and Y = 0 on the top.
  • Pixel Perfect Pane: displays the current visual screen that appears in the simulator.

    Use blue-green cross for coarse positioning. Drag the cross in the image and move the cross in Loupe accordingly. You can also click a point in the Pixel Perfect pane to move the cross to that point.

    The image corresponding to the View Object selected in the View Object pane is enclosed in a box to identify the position 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. View objects that are neither parent nor sibling are white boxes.

    The layout box can have other rectangles inside and outside it, each representing the View part. A purple or green rectangle indicates the View border box. A white or black box in the layout box representsPaddingIs the distance between the View object content and its border boxes. A white or black rectangle on the outside representsMarginsIs the distance between the View boundary 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 will generate a snapshot of the current screen. To implement it, clickSave as PNG. It will display a dialog box where you can select the Directory and file name of the file.

When you change the View object or enter another Activity, these panes are not automatically refreshed. To refresh the Pixel Perfect pane and Loupe pane, clickRefresh Screenshot. This operation will change the pane to display the current screen image. You may also need to refresh the View Object pane and clickRefresh Tree.

To automatically refresh the pane during debugging, set it at the top of the windowAuto RefreshAnd then useRefresh RateSet the refresh frequency.

Overwrite with Pixel Perfect

You often construct the UI based on a design bitmap image. The Pixel Perfect window allows you to load bitmap as the screen imageOverlayIt helps you match the View layout with bitmap images.

To overwrite a bitmap image, follow these steps:

  • Start your application on the device or simulator and navigate to the Activity you want to operate the UI.
  • Start Hierarchy Viewer and navigate to the Pixel Perfect window.
  • At the top of the window, clickLoad OverlayA dialog box is displayed, prompting you to load the image file. Then load the image file.
  • Pixel Perfect is displayed on the screen image in the Pixel Perfect pane. In the lower left corner of the bitmap image (X = 0, Y = max value), the pixels (X = 0, Y = max screen) that sit at the bottom of the screen are used as the anchor.

    By default, the coverage is 50% transparent, allowing you to see the screen image below. You can useOverlay:.

    By default, the overwrite is not displayed in the Loupe pane. Set Show in Loupe at the top of the window.

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

Display the screen snapshot of the Pixel Perfect window.

Use lint to optimize your UI

Android lint allows you to analyze and define the XML file of the application UI to discover the low performance of the view layer.

Android layoutopt has been replaced by the lin tool since SDK Version 16. The lint tool reports UI layout performance problems in a way similar to layout and discovers additional problems.

Original article:
Http://developer.android.com/tools/debugging/debugging-ui.html

This blog will continue to translate the following articles in the Debug series, followed by translated links:

Related Article

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.