Performance optimizations for Android (top)

Source: Internet
Author: User

Rendering mechanism for 1.Android UI

When we feel the flow of the picture, need to frame the number of frames to reach 40 frames per second to 60 frames. and a frame of time is about 16.67ms, in other words, in 1000ms of time, 16.67ms is about the reality of 60 frames per unit of time. In an Android system, the system triggers a rendering of the UI via a vsync signal, If the system every time the event is kept within 16.67ms, then we see the UI will be very smooth, which will require us to all the logic of the program within 16MS, if not done within 16ms, it will cause the phenomenon of dropped frames. That is, the currently redrawn frame is not processed by the logical blocking, such as a drawing task time 20ms, then the 16MS system issued by the VSYNC signal can not be drawn, the frame will be discarded, waiting for the next signal to start drawing, this is the reason for the picture lag.

The Android system provides tools for detecting UI rendering time, "GPU Rendering Mode Analysis" in "Developer Options" and "Display as a bar on screen", as shown below (this test machine is Meizu, other phones may be slightly different):

Each bar chart contains three parts, the blue part indicates the time to draw the display list, red represents the time required for OpenGL to render the display list, and yellow is the time the CPU waits for GPU processing. The Green Line in the middle represents the VSync time 16ms, and you need to keep all the bar charts under the green lines as much as possible.

2.overDraw

Overdraw means over-drawing, Refers to a frame of time (16.67ms) pixels are drawn multiple times, in theory, one pixel is best to draw once at a time, but because the overlapping layout causes some pixels to be drawn multiple times, each drawing will correspond to the CPU of a set of drawing commands and GPU operations, resulting in a waste of CPU and GPU resources. On the system's default drawing activity background, if the layout is drawn with overlapping backgrounds, then the background of the default activity is an invalid over-drawing.

In our developer options, there is a detection tool called GPU over-drawing, which can be used to determine the number of overdraw by the color on the interface after activating the function.

This tool helps us to detect the number of draws in the current area, thus optimizing the level of the interface drawing, maximizing the blue area and reducing the red area.

3. Optimize the layout hierarchy

In Android, the system measures the view, layout, and drawing, by traversing the view number. If the height of a view tree is too high, it can seriously affect the speed of measurement, layout and drawing, so the first way to optimize layout is to reduce the height of the view tree, Google also in the API documentation suggested that the view tree height should not exceed 10 layers. In the current root layout of the XML file, we default relativelayout to replace using Lineralayout as the default root layout because of the flat relativelayout to reduce the height of the layout tree resulting from lineralayout nesting , which improves the rendering speed of the UI.

1. Reusing layout with include tags

In an application interface, in order to maintain the unity of style, many interfaces will have a common UI, such as said Topbar,bottombar,actionbar and so on, if each interface is assigned to the common layout of the code, not only not conducive to post-code maintenance, It also increases the redundancy of the program. At this point, you can use the include tag to define a common UI.

<?xml version= "1.0" encoding= "Utf-8"?  <textview   Xmlns:android  = "http://schemas.android.com/apk/res/android"  android:layout_width  = "0DP"  android:layout_height  =" 0DP " android:t Extsize  = "28sp"  android:text  = "This is a common UI interface"  android:gravity  =" center ""  </textview  > 

In this common layout, I set layout_width and layout_height to 0DP so that the caller must be assigned the arrogance of the control when it is used, and no one will be able to see the control.

The next step is how to use the common UI layout. You only need to use the Layout property of the include tag in this common UI layout file to reference the ID of this common UI.

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"  Android:orientation="vertical"android:layout_width="Match_parent"android: Layout_height="Match_parent">                                              <include  layout  =< Span class= "Hljs-value" > "@layout/commen_ui"  android:layout_width  =< Span class= "Hljs-value" > "match_parent"  android:layout_height  = "wrap_content" />     <TextViewandroid:layout_width="Match_parent"android:layout_height= "Wrap_content" android:gravity="center"android:textsize="20sp"android:text= "                                         hello hello hello"/></linearlayout>

At this point, if we want to assign a value to a property in the layout, we need to overwrite an item property again and assign the value. The effect is as follows:

2. Using Viewstub to implement view lazy loading

Use the viewstub tag to implement a reference to a view and implement lazy loading. Viewstub is a very lightweight component that is not only visible, but is 0 in size, and below shows how to use viewstub to implement lazy loading.

First, create a layout, which is not required to be displayed when initializing the load, but only in certain situations, such as when viewing user information, the user details are displayed only when a button is clicked.
When we run the program, we find that the layout in viewstub does not show up, so how do we reload the displayed layout?
The component viewstub is first found through the Findviewbyid () method.

mStub = (ViewStub) findViewById(R.id.vs_viewstub);
接下来就有两种方式显示这个view:
    1. VISIBLE
      The view is displayed by calling Viewstub's Setvisiblity () method. The code is as follows:
mStub.setVisibility(View.VISIBLE);
    1. Inflate
      The view is displayed by calling Viewstub's inflate () method. The code is as follows:
View inflate = mStub.inflate();

Either way, you can re-expand the viewstub to show the referenced layout, and the only difference is that inflate () can return the referenced layout so that the corresponding control can be found through the View.findviewbyid () method.

View inflate = mStub.inflate();        TextView textview = (TextView) inflate.findViewById(R.id.textview);        textview.setText("我是点击后加载的");

Note: no matter only in that way, once the viewstub is set visible or inflate, viewstub does not exist, can not be repeated inflate, replaced by inflate layout, The ID of this layout is reset to the ID specified by the Android:inflateid property in Viewstub, which is why the error occurs after two hits: as shown below:

bya non-null ViewGroup viewParent

Briefly explain what the difference between View.gone and viewstub tags is? The common denominator is not displayed initially, but the viewstub tag only renders the entire layout when it is displayed, and View.gone is added to the layout tree file when the layout tree is initialized, which in contrast is more efficient viewstub.
As shown in the following:

4.hierarchyviewer.bat

Hierarchyviewer.bat cannot be used on a real machine, it can only be used on the emulator or on a native emulator, that is, a device that is not encrypted. Of course, the real machine is also available, you can go to GitHub to download an open source Project view Server, the following on the emulator using Hierarchyviewer.bat.
Hierarchyviewer.bat is located in the Sdk\tools directory, directly double-click to start, as follows:

Note : When we use hierarchyviewer, it must be that the simulator is open so that we can see the layout file we want to display in Hierarchyviewer.

Below we write a very redundant layout to display the file, the code looks like this:

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"  Android:layout_width="Match_parent"android:layout_height="Match_parent"  Android:orientation="vertical">                                              <linearlayoutandroid:layout_width="Match_parent"android:layout_height ="Match_parent">                        <linearlayoutandroid:layout_width="Match_parent"android:layout_height ="Match_parent">                                    <buttonandroid:layout_width="Match_parent"android:layout_height= "Wrap_content" android:gravity="center"android:text="Helloah ah ah ah ah"Android:textcolor ="#efff0019"android:textsize="20sp"/>                                                                                                        </linearlayout>    </linearlayout></linearlayout>

This layout file is a three layer linearlayout nested after a button, it is obvious that these linearlayout are redundant, using Hierarchyviewer can open this layout file, as shown in:


In general, we only focus on the Framlayout branch of the ID content, which is also the content of the Setcontentview () setting, which can be clearly seen in the layout file (red layout), which three layers LinearLayout no branch , the description is redundant nesting, can be directly removed.

When you click on one of the views, you can show the drawing of the view, but the first click when the various events are N/A, you need to click the Profile Node button in the menu to recalculate, to go back to the drawing information. In the lower right of the system will be given a different color of the small dots, used to indicate the efficiency of the drawing, green CCTV respectively represents a good difference in the drawing efficiency.

Performance optimizations for Android (top)

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.