android layout optimization and rendering optimization

Firstly, several optimization methods of android layout are introduced below. 1. include tag: You can allow another layout to be introduced in one lay...

Firstly, several optimization methods of android layout are introduced below.

1. include tag:

You can allow another layout to be introduced in one layout. For example, all interfaces of our program have a common part, which can then be extracted into a separate layout file, and each layout file references the common layout. (Title bar)

1. <?xml version="1.0" encoding="utf-8"?> 2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3. android:layout_width="match_parent" 4. android:layout_height="match_parent" 5. android:orientation="vertical" > 6. 7. <include layout="@layout/titlebar" /> 8. 9. ...... 10. </LinearLayout>


2. merge tag:

The merge tag is used as an auxiliary extension of the < include > tag. Its main function is to prevent redundant layout nesting when include refers to layout files.

 <?xml version="1.0" encoding="utf-8"?>  <merge xmlns:android="http://schemas.android.com/apk/res/android">   <Button  android:id="@+id/ok"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:layout_marginLeft="20dp"  android:layout_marginRight="20dp"  android:text="OK" />   <Button  android:id="@+id/cancel"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:layout_marginLeft="20dp"  android:layout_marginRight="20dp"  android:layout_marginTop="10dp"  android:text="Cancel" />   </merge>


3. ViewStub tag:

Load the layout only when needed. ViewStub is a kind of view, but it has no size, no drawing, and does not participate in the layout, so its drawing will not affect the layout.

 <?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" >   <EditText  android:id="@+id/edit"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:layout_marginBottom="10dp"  android:layout_marginLeft="20dp"  android:layout_marginRight="20dp"  android:layout_marginTop="10dp"  android:hint="@string/edit_something_here" />   <Button  android:id="@+id/more"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_gravity="right"  android:layout_marginRight="20dp"  android:layout_marginBottom="10dp"  android:text="More" />   <ViewStub  android:id="@+id/view_stub"  android:layout="@layout/profile_extra"  android:layout_width="match_parent"  android:layout_height="wrap_content"  />   <include layout="@layout/ok_cancel_layout" />   </LinearLayout>

 public void onMoreClick() {  ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub);  if (viewStub != null) {  View inflatedView = viewStub.inflate();  editExtra1 = (EditText) inflatedView.findViewById(R.id.edit_extra1);  editExtra2 = (EditText) inflatedView.findViewById(R.id.edit_extra2);  editExtra3 = (EditText) inflatedView.findViewById(R.id.edit_extra3);  }  }

Note: ViewStub and merge cannot be used together.


4. Delete useless controls and useless levels in the layout.

5. Linear Layout and Relative Layout should be used to meet the needs. If the function needs to be nested in LinearLayout, Relative Layout is preferred.

Why Linear Layout is better than Relative Layout: Linear Layout only needs one measure while Relative Layout needs two measurements when parsing xml.


2. Rendering optimization:

View's onDraw avoids a lot of operations, mainly in two aspects:

(1) In onDraw, do not create new local variables. Because the onDraw method may be called frequently (a large number of temporary objects not only occupy memory, but also lead to more frequent gc in the system, which reduces the efficiency of program execution).

(2) The onDraw method does not need to do time-consuming operations, nor can it do thousands of cyclic operations, because they will lead to poor view rendering.

4 July 2019, 21:54 | Views: 6605

Add new comment

For adding a comment, please log in
or create account

0 comments