Performance Optimization: Layout Optimization and Performance Optimization Layout

Source: Internet
Author: User
Tags android games

(Reprinted) Layout Optimization for performance optimization and Performance Optimization


Source: http://www.trinea.cn/android/layout-performance/

This article is the second article of Android Performance Optimization-layout optimization, which mainly introducesUse abstract Layout labels (include, viewstub, merge), remove unnecessary nesting and View nodes, and reduce unnecessary infalte and other Layout adjustable advantages, see the layout optimization tools (hierarchy viewer and lint).

 

The performance optimization topic has completed five parts:

Performance Optimization overview-performance problems and Performance Tuning Methods
Performance Optimization Article 3-Java (Android) code optimization
Performance Optimization Article 2-Layout Optimization
Performance Optimization Article 1-database performance optimization

Performance Optimization instance

 

1. Abstract layout labels 

(1) <include> label
The include label is often used to extract the public part of the layout for sharing by other layout to implement modular layout, which provides great convenience in layout compilation.
The following uses include in one layout main. xml to introduce another layout foot. xml as an example. The main. mxl code is as follows:

 
 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" > 5   6     <ListView 7         android:id="@+id/simple_list_view" 8         android:layout_width="match_parent" 9         android:layout_height="match_parent"10         android:layout_marginBottom="@dimen/dp_80" />11  12     <include layout="@layout/foot.xml" />13  14 </RelativeLayout>

The foot. xml introduced by include is the bottom of the Public page. The Code is as follows:

 
 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" > 5   6     <Button 7         android:id="@+id/button" 8         android:layout_width="match_parent" 9         android:layout_height="@dimen/dp_40"10         android:layout_above="@+id/text"/>11  12     <TextView13         android:id="@+id/text"14         android:layout_width="match_parent"15         android:layout_height="@dimen/dp_40"16         android:layout_alignParentBottom="true"17         android:text="@string/app_name" />18  19 </RelativeLayout>

<Include> the unique attribute required by a tag is the layout attribute, which specifies the layout file to be included. You can define the android: id and android: layout _ * attributes to overwrite the corresponding attribute values of the introduced layout root node. Note that after you redefine android: id, the top node I of the sub-layout changes.

 

(2) <viewstub> label
The viewstub label can be used to introduce an external layout like the include label. The difference is that the layout introduced by viewstub will not expand by default, that is, it will neither occupy the display nor the position, therefore, cpu and memory are saved during layout parsing.
Viewstub is often used to introduce the la s that are not displayed by default and only displayed under special circumstances, such as the progress layout, the refreshing layout displayed when the network fails, and the layout prompts when information errors occur.
The following uses network_error.xml as an example when a network error is added to the layout main. xml. The main. mxl code is as follows:

 
 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" > 5   6     …… 7     <ViewStub 8         android:id="@+id/network_error_layout" 9         android:layout_width="match_parent"10         android:layout_height="match_parent"11         android:layout="@layout/network_error" />12  13 </RelativeLayout>

Network_error.xml is the layout that must be displayed only when a network error occurs. It is not parsed by default. The sample code is as follows:

 
 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" > 5   6     <Button 7         android:id="@+id/network_setting" 8         android:layout_width="@dimen/dp_160" 9         android:layout_height="wrap_content"10         android:layout_centerHorizontal="true"11         android:text="@string/network_setting" />12  13     <Button14         android:id="@+id/network_refresh"15         android:layout_width="@dimen/dp_160"16         android:layout_height="wrap_content"17         android:layout_below="@+id/network_setting"18         android:layout_centerHorizontal="true"19         android:layout_marginTop="@dimen/dp_10"20         android:text="@string/network_refresh" />21  22 </RelativeLayout>

 

In java, find ViewStub through (ViewStub) findViewById (id), expand ViewStub through stub. inflate (), and then obtain the sub-View, as shown below:

 
 1 private View networkErrorView; 2   3 private void showNetError() { 4     // not repeated infalte 5     if (networkErrorView != null) { 6         networkErrorView.setVisibility(View.VISIBLE); 7         return; 8     } 9  10     ViewStub stub = (ViewStub)findViewById(R.id.network_error_layout);11     networkErrorView = stub.inflate();12     Button networkSetting = (Button)networkErrorView.findViewById(R.id.network_setting);13     Button refresh = (Button)findViewById(R.id.network_refresh);14 }15  16 private void showNormal() {17     if (networkErrorView != null) {18         networkErrorView.setVisibility(View.GONE);19     }20 }

 

ViewStub is expanded in showNetError () above, and networkErrorView is saved so that inflate does not need to be continued next time. This is the reduction of unnecessary infalte mentioned in the third part.

Most attributes of viewstub labels are similar to those of include labels.

Show some ViewStub code above

 
1 ViewStub stub = (ViewStub)findViewById(R.id.network_error_layout);2 networkErrorView = stub.inflate();

You can also write the following form

 
1 View viewStub = findViewById (R. id. network_error_layout); 2 viewStub. setVisibility (View. VISIBLE); // replace 3 networkErrorView = findViewById (R. id. network_error_layout); // obtain the expanded layout.

The results are consistent, but the display is not converted to ViewStub. Through the principle of viewstub, we can know that setting a view to GONE will not be parsed, thus improving the layout resolution speed, and the visibility attributes of VISIBLE and INVISIBLE will be parsed normally.

 

(3) <merge> label
When include is used, too many layout nesting and unnecessary layout nodes may be added, resulting in slow resolution, unnecessary nodes and nesting can be viewed through hierarchy viewer (which is described in the following layout optimization tool) or Settings> developer Options> display layout boundaries.

 

The merge tag can be used in two typical scenarios:
A. the layout top node is FrameLayout and does not need to set attributes such as background or padding. You can use merge instead, because the parent view tried by the Activity content is a FrameLayout, so you can use merge to eliminate only one.
B. when a layout is included by other la s as a sub-layout, merge is used as the top node of the layout. As a result, the top node is automatically ignored when it is introduced, merge all its subnodes into the main layout.

Take the example of the <include> label (1) as an example. Use hierarchy viewer to view the main. xml layout, for example:


We can find that there is an additional layer of unnecessary RelativeLayout, and change RelativeLayout in foot. xml to merge, as shown below:

 
 1 <?xml version="1.0" encoding="utf-8"?> 2 <merge xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" > 5   6     <Button 7         android:id="@+id/button" 8         android:layout_width="match_parent" 9         android:layout_height="@dimen/dp_40"10         android:layout_above="@+id/text"/>11  12     <TextView13         android:id="@+id/text"14         android:layout_width="match_parent"15         android:layout_height="@dimen/dp_40"16         android:layout_alignParentBottom="true"17         android:text="@string/app_name" />18  19 </merge>

 

Run hierarchy viewer again to view the main. xml layout, for example:

In this way, there will be no redundant RelativeLayout nodes.

 

2. Remove unnecessary nesting and View nodes
(1) set the nodes not needed for the first time to GONE or use viewstub
(2) Replace LinearLayout with RelativeLayout
About Android, the top node of main. xml in the new project is LinearLayout by default, and has been changed to RelativeLayout later, because RelativeLayout has better performance and can be implemented simply by nested LinearLayout.
For Android 4.0 and later versions, you can choose Settings> developer Options> display layout boundary to open the page layout display to see if unnecessary nodes and nesting exist. For versions earlier than 4.0, see hierarchy viewer.

 

3. Reduce Unnecessary infalte
(1) The layout of inflate can be directly cached. replace local variables with all the variables to avoid inflate again next time.
As shown in the ViewStub example above

 
1 if (networkErrorView != null) {2     networkErrorView.setVisibility(View.VISIBLE);3     return;4 }

 

(2) ListView provides the item cache. the standard format of adapter getView is as follows:

 1 @Override 2 public View getView(int position, View convertView, ViewGroup parent) { 3     ViewHolder holder; 4     if (convertView == null) { 5         convertView = inflater.inflate(R.layout.list_item, null); 6         holder = new ViewHolder(); 7         …… 8         convertView.setTag(holder); 9     } else {10         holder = (ViewHolder)convertView.getTag();11     }12 }13  14 /**15 * ViewHolder16 *17 * @author trinea@trinea.cn 2013-08-0118 */19 private static class ViewHolder {20  21     ImageView appIcon;22     TextView  appName;23     TextView  appInfo;24 }

 

For details about the ListView cache principle, see the Android ListView cache mechanism.

 

4. Other points
(1) Use SurfaceView or TextureView to replace normal View
SurfaceView or TextureView can improve performance by moving the Drawing operation to another independent thread.
The rendering process of a common View is completed in the main thread (UI thread). If some drawing operations affect performance, the optimization is poor. In this case, we can consider using SurfaceView and TextureView, their drawing operations occur on another thread outside the UI thread.
Because SurfaceView is outside the general view system, you cannot move, scale, or rotate a SurfaceView as you would normally try. TextureView is introduced in Android4.0. In addition to being drawn in a separate thread like SurfaceView, it can also be changed like a regular view.

 

(2) Use RenderJavascript
RenderScript is a language introduced by Adnroid3.0 to write high-performance code on Android. Its syntax is based on the C99 standard of C language and its structure is independent, therefore, you do not need to customize code for different CPUs or GPUs.

 

(3) drawing with OpenGL
Android supports high-performance plotting Using OpenGL APIs. This is the most advanced plotting mechanism available for Android and is widely used in applications with high performance requirements for games.
Android 4.3 supports OpenGL ES 3.0. Compared with 2.0, 3.0 has more buffer objects, new coloring languages, and multi-texture support, which will provide a better visual experience for Android games.

 

(4) try to create resources for all resolutions

Reduces unnecessary hardware scaling, which reduces the UI rendering speed.

 

5. layout optimization tools
(1) hierarchy viewer
Hierarchy viewer allows you to conveniently View the layout of the Activity. The time of attributes, measure, layout, and draw of each View is marked in red if it takes a lot of time. Otherwise, the Activity is displayed in green.
Hierarchy viewer. bat is located in the <sdk>/tools/directory. See Using Hierarchy Viewer, as shown in the following figure:

Note: hierarchy viewer is currently unavailable for the commercial version:

To preserve security, Hierarchy Viewer can only connect to devices running a developer version of the Android system

See http://lxfgrace.iteye.com/blog/1821869, just try

 

(2) layoutopt
Layoutopt is a command line that provides layout and Its Hierarchical Optimization prompts. It has been replaced by lint after sdk16, choose Windows> Show View> Other> Android> Lint Warnings to View the lint optimization prompt. For details about lint, see Improving Your Code with lint.

 

Source: http://www.trinea.cn/android/layout-performance/

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.