Optimization of View rendering in Android ---- optimizing layout Layers

Source: Internet
Author: User

Translation

 

 

 

I have been working on the screen lock interface recently. I have also written some simple principles about the screen lock interface before. I didn't want to really understand it myself.

I found that the screen lock framework is big and complex, mainly reflected in the following two aspects:

 

1. Interface Composition and update mechanism;

2. For power management control, the system's power management will be disabled on the screen lock interface, and the screen brightness control will be taken over by yourself.

 

Of course, there are more logic details to be processed, so we can only study it with patience ..

 

Through the processing of the screen lock interface, I found that I am still not familiar with the View painting, and many things have not been carefully studied, resulting in

The project is really messy. Therefore, with this opportunity, we also put the advanced knowledge of Android 4.0 developer (Shan Ren has been

Immersed in Android 2.2.

 

 

To avoid ambiguity, we first describe the meaning of Android "Layout" once in three aspects:

1. collectively referred to as how to place the UI and display the UI;

2. layout file:/res/layout/xxx. xml;

3. layout process: layout Process during Android rendering;

4. Some layout controls, such as LinearLayout and FrameLayout;

 

 

 

Body:

 


Layout improved mace)
 

 


Layout is an important part of the Android application, which is directly related to the user experience. If a layout is poor, it will generate

Memory consumption and inefficient UI applications. Android SDK and its tools can help you locate problems hidden in the layout process.

With these courses, you can achieve smooth and smooth interfaces at a low memory cost.

 

The course is as follows:

 

1. Optimize layout levels

Similarly, a complex web page will prolong the loading time. If your layout level is too complex, it will also lead to efficiency problems. This course

We will show you how to use SDK tools to observe your layout and discover bottlenecks in the layout process.

 

2. Use the <include/> label to reuse the layout file.

If the application UI repeats some layout structures in multiple places, this course shows you how to create an efficient and reusable layout structure, and then

Include them with appropriate UI layout files.

3. Load View as needed

In addition to simply including a layout component in another layout file, you may want to display the view as needed. Sometimes

After the Activity is run. This course shows you how to improve the layout initialization behavior-loading a view of the layout file as needed.

4. How to smoothly slide the ListView

If you build a ListView instance to present list items that contain complex or large data volumes, this may affect the smoothness of ListView.

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

TRANSLATION 1:

 


Optimize layout Layers
 

 


A common misconception is that basic layout structures (such as LinearLayout and FrameLayout) can be used in most cases.

Produce an efficient layout. Obviously, every control and layout added to your application must be initialized, layout ),

Draw ). For example, embedding a LinearLayout produces a layout level that is too deep. More seriously, embed several

LinearLayout using the layout_weight attribute will cause a large amount of overhead, because each subview needs to be measured twice. This is repeated Parsing

It is important to layout a file, for example, when used in ListView or GridView.

 


Observe your layout
 

The Android SDK toolkit includes a tool called "Hierarchy Viewer" that allows you to analyze the program running your application.

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

 

The "Hierarchy Viewer" tool allows you to select a running process from a connected device or simulator and then display the layout Hierarchy Tree.

(Layout tree ). The traffic lights under each square block (SEE) --- red, green, and blue show the measurements, layout, and

(Draw) Efficiency Value in the process, which helps you locate potential problems.

 

Assume that an Item in ListView has the following layout (see Figure 1:

 


Figure 1: Layout of an Item in ListView

 


The "Hierarchy Viewer" tool can be found in the <sdk>/tools path. When you open it, the "Hierarchy Viewer" tool displays

All available devices and processes running on these devices. Click "Load View Hierarchy" to display the UI layout of a selected component.

Level. For example, Figure 2 shows the layout Hierarchy Tree in Figure 1.

 


Figure 2: Use the layout tree of LinearLayout

 


In Figure 2, you can see that there are some problems with the layout structure of the three layers. The click items indicate that each measurement (measure ),

Layout and the time consumed during drawing (draw) (see figure 3 ). Obviously, this item (LinearLayout) takes the longest time

For measurement, layout, and drawing, you should spend some time optimizing them.

 


Figure 3: Draw time of a LinearLayout

 


The rendering time of the layout file is as follows:

Measurement process: 0.977 ms

Layout: 0.167 ms

Rendering process: 2.717 ms

 

Modify layout File
 

Low layout efficiency is due to the fact that an embedded LinearLayout control uses a flat layout file to make the layout more efficient.

It is lighter and wider, rather than narrower and deeper, so that efficiency can be improved. A RelativeLayout can also serve as the root node.

(That is, figure 1 ). Therefore, when RelativeLayout is used to change the layout design, we can see that there are only two layers of layout.

The new layout Hierarchy Tree is as follows:

 


Figure 4: Layout tree using RelativeLayout

 

Now, the rendering time of the layout file is:

Measurement process: 0.977 ms

Layout: 0.167 ms

Rendering process: 2.717 ms

Maybe it is only a small improvement, but this time it will be called multiple times, because ListView will layout all items and accumulate,

After improvement, the effect is quite impressive.

 


Most of the time difference is due to the use of LinearLayout with the layout_weight attribute, which can speed down the measurement process. This is only

Is an example, that is, each layout should be used properly and you should carefully consider whether it is necessary to adopt the "layout_weight" attribute.

 

Use the Lint tool (Translator's note: The ADT plug-in is updated to the latest post-16.0 tool)

 

For more information about Lint usage, see Android Lint (official code optimization tool).

A good practice is to use the Lint tool in your layout file to find ways to optimize the layout hierarchy. Lint has replaced Layoutopt

Tool and it provides more powerful functions. Some Lint rules are as follows:

 


1. Use a composite control-including an ImageView and a TextView control's LinearLayout.

Composite controls will be processed more effectively.

2. Merge frames as the root node layout (Framelayout)-if a frame layout is used to layout the root node in the file, and it has no background image

Or padding. The more effective method is to use the <merge/> label to replace the <Framelayout/> label.

 


For more information, see <android merge layout>

<Use of merge and include tags in Android>

3. Useless leaf nodes-generally, if a layout control does not have a subview or background image, 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 no sibling view node, this view is not a ScrollView control or

Root node, which has no background image and can also be removed. after removal, all child views of the parent view are directly migrated to the previous parent view.

. It also makes the resolution layout and layout layers more effective.

 

5. A deep layout layer-too many embedded la s are 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 using the Eclipse Environment for Development, Lint can automatically solve some problems, provide some suggestions, and directly jump to the error code for verification.

If you do not use Eclipse, Lint can also be run through the command line.

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.