Android App performance Optimization (i) Layout optimization

Source: Internet
Author: User

When creating complex layouts, we add a lot of viewgroup and view to the XML file. With each iteration, the view tree becomes more and more deep, the interface loads more and more slowly and consumes more memory. Your program needs to be optimized when your program appears to be temporarily black when loading, or when a short black screen is temporarily switched on or off, or if there are problems such as memory overflow (OOM).

So how do you make the program run faster? Respond more quickly? Optimizing layout is one of the most basic methods, this article will introduce the most basic optimization layout method.

1. Use Viewstub to implement view lazy loading.

In many cases, the initial state of some of the view in the XML layout file is set to not display (android:visible= "invisible" or "Gone"). If a view of this setting exists in your code, you can try using Viewstub.

The viewstub is described in the Android development documentation as follows:

Http://developer.android.com/reference/android/view/ViewStub.html

"Viewstub" is a non-visible and size 0 attempt that can be deferred to a run-time load (inflate) layout resource. Layout resources are loaded when viewstub is set to visible or when the inflate () method is called. The viewstub is then replaced by the loaded layout.

Now we use Eclipse to create a new project, the default fragment layout file is as follows:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
tools:context= "Com.example.testlayout.mainactivity$placeholderfragment" >

<button
Android:id= "@+id/button"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparenttop= "true"
android:text= "Inflate View Stub"/>

<linearlayout
Android:id= "@+id/linearlayout"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:orientation= "Vertical"
android:layout_below= "@id/button" >

<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>
</LinearLayout>

</RelativeLayout>

As seen above, multiple sub-view is nested in LinearLayout. If you do not need to display this part immediately when the activity is loaded, we can use viewstub to replace the linearlayout.

The modified layout is as follows:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
tools:context= "Com.example.testlayout.mainactivity$placeholderfragment" >
<button <!--Click the Button and then load viewstub
Android:id= "@+id/button"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparenttop= "true"
android:text= "Inflate View Stub"/>

<viewstub
Android:id= "@+id/viewstub"
Android:inflatedid= "@+id/view_stub_layout" <!--will replace the ID of the outermost viewgroup in the sub-layout
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_below= "@id/button"
android:layout= "@layout/view_stub_layout"/> <!--loaded sub-layout--
</RelativeLayout>

The omitted linearlayout is placed in the new layout file view_stub_layout.xml , which reads as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/linearlayout"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:orientation= "Vertical" >
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>
</LinearLayout>

Finally complete our Java file and when you click on the button, execute the following code:

private void Showviewstub () {
Viewstub stub = (viewstub) Mrootview.findviewbyid (r.id.viewstub);
if (stub! = null) {
LOG.I ("Xzy", "stub is not null");
Stub.setlayoutresource (r.layout.view_stub_layout); If the layout file is not setandroid:layout= "@layout/view_stub_layout", you can set sub-layouts like this
Stub.inflate ();
View view = (view) Mrootview.findviewbyid (r.id.linearlayout);
if (view! = null) {
View.setbackgroundcolor (color.red);
}
} else {
LOG.I ("Xzy", "Stub is Null");
}
}

After the viewstub is loaded, it becomes null and cannot be loaded by the Viewstub object, so it does not load again.

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.