Layout Optimization for android applications
When developing an APP, we should optimize the App not only in code implementation, but also in our interface layout. If the layout is low, the layout loading speed of the system will be very slow, making the user experience very poor. This article mainly summarizes the layout optimization in my usual aspects, I think there are some skills and techniques that can be used frequently in layout optimization.
1. Reduce the nesting of the layout, which is also the most important
Android users know that the entire UI layout file of android must be parsed into a View object layer by layer. If the layout is too deep, this will have a great impact on the resolution speed of recursive layers. Therefore, we must not make the layout file layers too deep. If we want to make the layout file layers not deep, commonly used methods include: 1) Replace LinearLayout with RelativeLayout. 2) when compiling the layout file, you can use the HieracyView tool to check whether there are unnecessary la S. If yes, you must remove useless la S.
2. layout reuse.
Some common la s do not need to be rewritten every time. You can write them into an independent layout file and use the include tag to reference the layout. However, strictly speaking, layout reuse only reduces the amount of code written and cannot optimize the application. In addition, it is easy to generate useless parent layout in the first point when using include, for example:
The following describes how to remove the negative effects of the include generation.
3. Use the merge label to eliminate the useless layout introduced by the include label.
When using merge, pay attention that the merge label can only be the root layout. Let's take a look at the following two points for an example:
First, create a root layout for the merge tag. The Code is as follows:
?xml version="1.0" encoding="utf-8"?>
Next, use include to introduce the layout:
4. Use ViewStub to load unused la S. In some cases, android: visibility = "gone" is replaced, because the layout that is dropped by gone will create objects at the same time. So why is ViewStub efficient? Let's talk about the source code. Let's take a look at the source code of ViewStub:
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(0, 0); }
@ Override
Public void draw (Canvas canvas ){
}
From the onMeasure () method and the draw () method, we can see that the initial width and height of ViewStub are both zero, so it does not occupy space. Secondly, the draw () method does not execute any painting, the two methods show that ViewStub is indeed very efficient.
When you want to manipulate ViewStub in the code, you must first use the viewstub. inflate () method to initialize the View. Otherwise, a null pointer error is returned.
5. reduce background re-painting between different layers. For example, if the background of a view parent layout has been set to white, you do not need to set the background color for this view, this can greatly increase the speed.
6. if the entire App uses a custom Title and background, we can use a custom style to permanently remove the title and background added by default to our Activity, it can also increase the rendering speed of the Activity.