One of the view drawing optimizations in Android----optimize the layout hierarchy

Source: Internet
Author: User

This article original, reproduced please indicate the source :http://blog.csdn.net/qinjuning

Preface, unexpectedly is translation, of course, must get rhythmic. According to the great writer format,--.

Order of Translation

Recently has been doing the lock screen interface, before also written about the lock screen interface of some simple principles, did not want to really deep understanding of the lock screen, only

found that the lock screen frame is really large and complex, mainly reflected in the following two aspects:

1, the interface of the composition and update mechanism;

2, the control of power management, the lock screen will disable the system's power management, take over the screen brightness control.

Of course, there are more logical details to deal with, can only withstand the temperament to study.

Through the lock screen interface processing, only to find their own view of the painting or not ripe, a lot of things did not go into painstaking research, resulting in their own

It's just a rush when you really do the project. Therefore, by this opportunity, also put Android 4.0 developer these advanced knowledge (the hermit has been

Immerse in Android 2.2) gave it a bit, it's a lot of fun.

Start: In order to avoid ambiguity, the Android "Layout" The meaning of the first time to explain, mainly the following three aspects:

1, collectively, that is, how to display ui,ui rendering effect, etc.;

2, layout documents, namely/res/layout/xxx.xml;

3, layout process, Android drawing process of the layout process;

4, some layout control, such as LinearLayout, framelayout and so on;

Body:

Improved layout efficiency (layouts performace)


This article translated address:http://developer.android.com/training/improving-layouts/index.html

Layouts are an important part of Android applications and are directly linked to the user experience. If a layout is bad, it will produce a

Consume memory with inefficient UI applications. The Android SDK and the tools it contains can help you locate problems that are hidden during the layout process by

With these lessons, you can achieve a smooth and smooth interface with a small memory cost.

The courses are as follows:

1 . Optimize the layout level

Similarly, a complex web page can extend the load time, and your layout hierarchy may cause some efficiency problems if it is too complex. This course

Tell you how to use the SDK tools to observe your layout and find bottlenecks in the layout process.

2. Using <include/> Label Reuse layout file

If your application's UI repeats certain layout structures in multiple places, this course shows you how to create an efficient, reusable layout structure, and then

Include them in the appropriate UI layout file.

3. Load view view on Demand

In addition to simply including a layout component in another layout file, you may want to visualize the view when you need it, sometimes

After the activity is running. This course tells you how to improve the layout initialization behavior----Load a view of the layout file on demand.

4. How to make the ListView slide smoothly

If you build a ListView instance to render those list items that contain complex or large volumes of data, this may affect the flow of the ListView

Sliding. This course provides some suggestions on how to make the sliding process smoother.

Translation one:

Optimize the layout hierarchy

This address: http://developer.android.com/training/improving-layouts/optimizing-layout.html

A common misconception is that using basic layout structures (for example: LinearLayout, framelayout, etc.) can in most cases

produce an efficient layout. Obviously, every control and layout you add in your application needs to be initialized, laid out,

Draw (Drawing). For example, embedding a linearlayout creates a layout hierarchy that is too deep. More seriously, embedding several

LinearLayout with the Layout_weight attribute will result in a lot of overhead, because each child view needs to be measured two times. This is repeated parsing

A significant point in the layout of a file, such as when used in a ListView or GridView.

Watch your layout.

The android SDK Toolkit includes a tool called " Hierarchy Viewer" that allows you to analyze when your application is running

Layout. By using this tool, you can help you find bottlenecks in your layout efficiency.

the Hierarchy Viewertool allows you to select a running process in a connected device or emulator and then render the layout hierarchy tree

(Layout tree). Traffic lights under each square block (see)---red-green-blue shows the measurement (measure), Layout, and drawing

(draw) process, which can help you locate potential problems.

Suppose an item in the ListView has the following layout (see Figure 1):

Figure 1:listview The layout of an item

TheHierarchy Viewer tool can be found under the <sdk>/tools path. When you open it, the Hierarchy Viewertool displays the

All available devices and processes running on those devices. Click "Load View Hierarchy" To display the UI layout of a component of your choice

Level. For example, figure 2 shows the layout hierarchy tree for Figure 1.

Figure 2: A layout tree using LinearLayout

in Figure 2, you can visually see that there are some problems with this three-layer layout structure. Click on the item to reflect the measurement (measure),

Layout, and time consumption during drawing (draw) (see Figure 3). It was clear that the item (LinearLayout) took the longest time to

To measure, layout, and draw, you should devote some effort to optimizing them.

Figure 3: Drawing time for a linearlayout

the time to finish rendering the layout file is:

Measuring process: 0.977ms

Layout process: 0.167ms

Drawing process: 2.717ms

Modify a layout file

because the efficiency of the layout is low because of an inline linearlayout control, by flattening the layout file ----Let the layout become

Lighter and wider, rather than becoming narrower and deeper, this increases efficiency. A relativelayout as a root node can also provide

Layout effect (that is, figure 1). So, using Relativelayout to change the layout design, you can see that now our layout level is only 2 layers.

The new layout hierarchy tree is as follows:

Figure 4: A layout tree using Relativelayout

The time to finish rendering the layout file is now:

Measuring process: 0.977ms

Layout process: 0.167ms

Drawing process: 2.717ms

Maybe it's just a little bit of an improvement, but this time it's going to be called multiple times because it's the ListView that will lay out all of the item and accumulate it.

The improved effect is still very impressive.

Most of the time differences are due to the use of LinearLayout with the Layout_weight attribute, which slows down the measurement process. This is only

is an example where each layout should be properly used and you should seriously consider whether it is necessary to use the "Layout_weight" attribute.

Using the Lint tool (translator Note: ADT Plugin updated to the latest 16.0 tool)

More about the use of Lint:"Android Lint (Official Code optimization tool)"

A good practice is to use the lint tool in your layout file to find a way to optimize the layout hierarchy. Lint has replaced layoutopt.

Tool and it provides more powerful functionality. Some lint rules are as follows:

1. Using the combo control ---contains a ImageView and a linearlayout of a TextView control if it can be used as a

The combined control will be handled more efficiently.

For this, see more : http://stackoverflow.com/questions/8318765/ How-do-i-use-a-compound-drawable-instead-of-a-linearlayout-that-contains-an-imag

2. Merge the frame layout (framelayout) as the root node---- If a frame layout is in the root node of the layout file, and it does not have a background picture

Or padding, the more efficient way is to replace the < Framelayout/> tag with the <merge/> tag.

for this, see more: <<android merge Layout >>

use of the merge and include tags in <<android >>

3. Useless leaf node----- usually if a layout control has no child view or background picture, the layout control can be removed

(because it is in the invisible State).

4. Useless parent Node----- If a parent view has a child view, but does not have a sibling view node, the view is not a ScrollView control or

root node, and it has no background picture and can be removed, after removal, all child views of the parent view are migrated directly to the previous parent view

Hierarchy of layouts. The same can make parsing layouts and layout hierarchies more efficient.

5, too deep layout level---- embedded too much layout is always inefficient. Consider using some flat layout controls, such as Relativelayout,

GridLayout to improve the layout process . The default maximum layout depth is 10.

When developing with eclipse environment, Lint is able to solve some problems automatically, provide some suggestions and go directly to the code to check for errors.

If you are not using Eclipse,lint, you can run it from the command line. For more information on Lint, see: "Android Lint"

Hi ~ ~ ~

One of the view drawing optimizations in Android----optimize the layout hierarchy

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.