Improve the speed of the Android user experience Trilogy

Source: Internet
Author: User

Some time ago, we introduced several official Google documents. This article aims to improve AndroidUser ExperienceThe trilogy provides excellent user experience with three features: fast speed, timely response, and seamless. The following information helps your applicationAndroidTo implement these features. We will explain how to make your application faster. We will introduce you to you in the future about timely response and seamless.

Fast

You can't assume that your mobile phone is as fast as your desktop and server, but you need to pay more attention to whether your code is efficient.

Two principles should be followed for writing efficient Android code:

Do not do unnecessary things

Do not allocate unnecessary memory

The following are some tips for achieving this goal. Some tips conflict with oo principles and are applicable to specific scenarios ):

1,Avoid object Creation

For example, the int array is better than the Integer array. Likewise, this applies to the combination of all basic types.

2,Use local methods

Do not use special methods (specialty methods) such as String. indexOf () and String. lastIndexOf ). These methods are implemented using C/C ++.

3,Good analogy Interface

 
 
  1. Map myMap1 = new HashMap();  
  2. HashMap myMap2 = new HashMap(); 

Calling an interface takes twice as long as calling an object class.

4,Getter and setter are not required

Variables should be accessed directly

5,Cache member variables locally

 
 
  1. for (int i = 0; i< this.mCount; i++)             
  2.     dumpItem(this.mItems[i]); 

It is best to change it to this:

 
 
  1. int count = this.mCount;  
  2. Item[] items = this.mItems;  
  3. for (int i = 0; i< count; i++) dumpItems(items[i]); 

In addition, never call any method in the second condition of

6,Add final to the constant

 
 
  1. static int intVal = 42;  
  2. static String strVal = “Hello, world!”; 

The compiler will generate a method called the initialization class. This method will be executed when the class is used for the first time. The method will assign 42 to intVal, and then assign a reference pointing to a common table in the class to strVal. When these values are used in the future, they will be found in the member variable table.

 
 
  1. static final int intVal = 42;  
  2. static final String strVal = “Hello, world!”; 

Currently, classes do not need methods because constants are directly saved to class files during initialization of member variables. The code that uses intVal is directly replaced with 42, and strVal points to a String constant instead of a member variable.

7,Use foreach with caution

Foreach can be used in the collection type that implements the Iterable interface. Foreach assigns an iterator to these objects and then calls the hasNext () and next () methods. You 'd better use foreach to process the ArrayList object, but for other set objects, foreach is equivalent to using iterator

8,Avoid Enumeration

It is very convenient to enumerate variables, but unfortunately it will sacrifice the execution speed and greatly increase the file size.

9,Declare the external variables or methods to be accessed by the internal class within the package range.

 
 
  1. public class Foo {  
  2. private int mValue;  
  3. public void run() {  
  4.     Inner in = new Inner();  
  5.     mValue = 27;  
  6.     in.stuff();  
  7. }  
  8. private class Inner {  
  9.     void stuff() {  
  10.     System.out.println(Foo.this.mValue);  
  11.     }  
  12. }  

Foo $ Inner is a completely independent class. It is illegal to directly access the private members of Foo. The compiler automatically generates a method:

 
 
  1. /*package*/ static int Foo.access$100(Foo foo) {  
  2. return foo.mValue;  

The internal class calls this static method every time it accesses the "mValue" method. Similarly, the same is true for internal classes to access private methods.

10,Avoid floating point number

Embedded processors generally do not support floating-point computing hardware. All the operations on "float" and "double" are implemented through software.

We can avoid this problem by changing the variable and function declaration for internal class access from private range to package range. This can make the code run faster and avoid generating additional static methods. (Unfortunately, these member variables and methods can be directly accessed by other classes in the same package, which is contrary to the classic OO principle. Therefore, you should exercise caution when designing this optimization principle)

Well, this is the fast speed of the Android user experience trilogy we have prepared for you. In the future, we will introduce timely response and seamless response.

Related Article

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.