Android improves UI performance tips

Source: Internet
Author: User

The way to improve UI performance is actually very much in the actual development has been used, here to summarize.

1. Reduce the clogging time of the main thread

If an operation takes a long time (more than 5 seconds), we should put it in a background thread to run it. Notify the main thread to make changes only when you need to change the UI interface.

Android has provided a way to Asynctask to generate new asynchronous tasks from the main thread.

2. Improve the efficiency of Adapter&adapterview

(1) Reusing a generated item View

(2) Add Viewholder to avoid repeatedly searching for controls that need to be changed

Public View GetView (int position, View Convertview, ViewGroup parent) {    Viewholder holder;    if (Convertview = = null) {         Convertview = minflater.inflate (R.layout.list_item_icon_text, null);         Holder = new Viewholder ();         Holder.text = (TextView) Convertview.findviewbyid (r.id.text);         Holder.icon = (ImageView) Convertview.findviewbyid (R.id.icon);         Convertview.settag (holder);    } else {         holder = (viewholder) convertview.gettag ();    }    return Convertview;} Static class Viewholder {    TextView text;    ImageView icon;}
3. Optimize the layout file, prevent the level nesting too deep, preferably not more than 5 layers

(1) Tools to use the viewing layout: Hierarchy Viewer

(2) Using the Layout optimization tool: layoutopt

4. Optimize Activity Background Map

First of all we need to know that in Android activity, there are not only the view that you use when you use Setcontentview, but also some other view.


The root view is a decorview, and the view you set is included in the, the ID is content, for example, as seen:


In the default case. The Decorview includes a default background map, which we will use as a starting point. If we need to set up a full-screen background image in activity. You should not add ImageView or set the background map in layout. Instead, you should call Setbackgrounddrawable () to change the background image of the Decorview.

When you do not need to use the background map, we should clear the background map
(1) Code mode:

GetWindow (). Setbackgrounddrawable ()

(2) XML form

Create file res/values/theme.xml<resources>    <style name= "Theme.custombackground" parent= "Android:Theme" >        <item name= "Android:windowbackground" > @drawable/image</item>    </style></ resources>//can be set to null or the background map you need, depending on the need to Windowbackground
Add Android:theme= "@style/theme.custombackground" to the <activity/> or <application/> tag
5. Using Viewstub

Viewstub is an invisible, lightweight view. It has no size, does not draw and participates in some form in the layout. Only the view that is called after inflate is instantiated, which means that the cost of preserving the structure of the view hierarchy with Viewstub is very low.

See the following links for usage:

http://blog.csdn.net/hitlion2008/article/details/6737537

Android improves UI performance tips

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.