Android application performance optimization – startup acceleration

In the recent study of Android performance optimization, the first to solve the problem of jumping Caton when opening a web page in the application, trying to introduce third party WebView components, but introducing another problem, the initialization of the third party WebView component is placed in the Application, which leads to the delay of the App startup time. Long. Today, we will talk about how to optimize acceleration from two aspects of Application and Activity.

The start time of a.Application acceleration App is the time the user clicks on app icon to the first interface of the app to give the user the time it takes, shortening this time and quickly displaying the first interface to the user, which can greatly improve the user’s experience. There are two main ways to optimize Application, one is to reduce the execution time of the onCreate method in Application, and one is to speed up the first interface with theme and Drawable.

1. reduce the execution time of the onCreate method

Using Android studio to build an application, we will find that the start speed is very fast, but as the complexity of the application increases, the integration of the third party components is increasing, and the initialization of the third party components is increasing in onCreate. This is a clear discovery that the start of the App is carton, the first interface is presented. Before the white screen or black screen time increased, this is due to the onCreate execution time is too long. To solve this problem, IntentService can be used to handle time-consuming initialization operations.

The code of IntentService is as follows:

Public static void start (Context context) {Intent intent = new Intent (context, DwdInitService.class);};}

@Override protected void onHandleIntent (Intent intent) {if (intent! = null) {final String action = {}; {} {};}}}

In Application, start can

DwdInitService.start (this);

A X5WebView initialization operation is made, and the effect is quite obvious.

2. optimize the presentation of the first interface

As I mentioned earlier, when a App starts, there will always be a white screen or a black screen. It is particularly bad from the user experience. How to eliminate this white screen? Here we can use custom theme and Drawable to solve this problem. Here we use a simple demo as a case: the following renderings

Layout code, as follows

< ImageView android:layout_width=" wrap_content" android:layout_height=" wrap_content" android:layout_gravity=" center" android:layout_marginBottom=" 24dp" android:src=" @drawable/ic_launcher&;

The code is simple, but every time you start, you will find that there will be a white screen before the page is displayed. Now the code is transformed as follows

A. defines a Drawable of loading.xml

Setting up the background and logo pictures here

B.style defines a theme, and windowBackground sets the background to loading.xml.

& lt; style name=& quot; Theme.Default.NoActionBar& quot; parent=& quot; @style/AppTheme& quot; & plurality; dialectical; dialectical; dialectical; dialectical;

C. sets the defined theme to LoadingActivity

Ok, completed, now start App, white screen is missing, user experience has also been improved.

Optimal acceleration of two.Activity

After entering App, the speed of the jump between pages is also an important part of the user experience, such as opening an embedded web page, after clicking the trigger button, a card will appear after the jump to the past.

The optimization of a Activity is also to reduce the execution time of the onCreate method. The onCreate method often consists of two parts. First, setContentView () is used to implement the layout, the first is to initialize and fill data in onCreate.

The second point is easier to understand, and the time consuming data reading and computing work is minimized in onCreate, and the asynchronous callback can be used to reduce the occupancy of the UI main thread.

Now, from setContentView, each of the controls in the layout needs to be initialized, arranged, and drawn, which are mostly time consuming operations to reduce the display speed. And in the case of no time-consuming data manipulation in onCreate, monitoring setContentView () through the TraceView tool almost takes up 99% of all time from the beginning of onCreate () to the end of onResume ().

Reduce the time spent on setContentView:

1. reduce layout nesting level

A. uses relative layout

Reduce the use of linear layout, use relative layout as far as possible, and reduce nesting levels. Nested multiple LinearLayout instances that use layout_weight properties will cost more, because each of the sub layouts will measure two times; although the relative layout is tedious, it can reduce the nesting level and reduce the drawing time.

B. use

Label merge layout

Use

The label merge layout reduces the layout level, but the merage tag works only for the FrameLayout layout, and the parent of the Activity content view is a FrameLayout. The merge tag code is not used as follows: the code using the merge tag.

After the merge tag is used, the layout level is reduced accordingly. C. controls its own properties by controlling its own properties, reducing nesting levels, such as common linear arrangement menu layout, as follows

The code is implemented in LinearLayout as follows:

& lt; LinearLayout android:layout_width=& quot; match_parent& quot; android:layout_height=& quot; 62dip& quot. EW android:layout_width=& quot; wrap_content& quot; android:layout_height=& quot; wrap_content& quot; android:src=& quot. Out_width=& quot; match_parent& quot; android:layout_height=& quot; wrap_content& quot; android:layout_marginLeft=& quot; 15dp& Ot; android:textSize=& quot; 18sp& quot; /& gt; & lt; /LinearLayout& gt;

The drawableRight code for using the properties of TextView is as follows

& lt; TextView android:id=& quot; @+id/my_order& quot; android:layout_width=& quot; match_parent& quot. Quot; 15dip& quot; android:gravity=& quot; center_vertical& quot; android:paddingLeft=& quot; 28dip& quot;

The amount of code and nesting levels are reduced accordingly, and the effect is perfect.

2. using ViewStub delay expansion

ViewStub is a lightweight and invisible view. When needed, it can be used to postpone the expansion of the layout in your own layout. It is also a way to expand the layout by inflate when you need to expand and expand the layout in onResume by setting the flag bit.

Features: (1).ViewStub can only be Inflate once, and then ViewStub objects will be emptied. According to the sentence, if a layout specified by ViewStub is Inflate, it will not be controlled by ViewStub again. (2).ViewStub can only be used for Inflate layout file instead of a specific View. Of course, View can be written in a layout file. Usage scenario: (1) during the running of a program, a layout will not change after Inflate, unless it is restarted. (complex layout) (2). To control display and hide is a layout file instead of a View. Case: after optimization with ViewStub, the expansion time can be reduced from 1/2 to 2/3.

Use the code to expand the layout in the onCreate () method.

ViewStub viewStub = (ViewStub) findViewById (R.id.viewstub_demo_image); viewStub.inflate ();

Leave a Reply

Your email address will not be published. Required fields are marked *