Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How does Android development make the user experience faster?
How does Android development make the user experience faster?
An excellent user experience has three characteristics: speed, timely response and seamless. The following information will help your application implement these features on Android. We will explain in detail how to make your application fast, and we will introduce it to you later on about timely response and seamless.

quick

You can't assume that mobile phones are as fast as desktop systems and servers. More importantly, you should pay attention to whether your code is efficient.

Writing efficient Android code should follow two principles:

Don't do unnecessary things.

Don't allocate unnecessary memory.

Here are some techniques to achieve this goal (some of them conflict with oo principle, so use scenarios appropriately):

1. Avoid creating objects

For example, int array is better than Integer array, which also applies to all basic types of combinations.

Step 2 Use local methods

Don't hesitate to use String.indexOf ().

Professional methods, such as String.lastIndexOf (). These methods are all implemented in C/C++

3. Good use of analog interface.

Java code:

Map myMap 1 = new

HashMap();

HashMap my map 2 = new HashMap();

Calling a reference to an interface will take twice as long as calling a reference to an entity class.

4. There are no getter and setter

You should access variables directly.

5. Cache member variables locally.

Java code:

for(int I = 0; I & ltthis.mCounti++)

dump item(this . MIT EMS);

//Better change it to this:

int count = this.mCount

item[]items = this . MIT EMS;

for

(int I = 0; I < count; i++)dump items(items);

6. Add final to the constant

Static int intVal =

42;

1 . static String strVal = " Hello,world!" ;

The compiler will generate a method called Initialization Class, which will be executed the first time the class is used. This method assigns 42 to intVal, and then assigns a reference to the constant table in the class to strVal. When using these values in the future, you can find them in the member variable table.

Static final int intVal = 42

1. Static final string strVal =

"Hello, world!" ;

Now, classes no longer need methods, because when member variables are initialized, constants are saved directly to the class file. Code using intVal is directly replaced with 42, while code using strVal points to a string constant instead of using a member variable.

7. Use foreach with caution

Foreach can be used to implement the collection type of the Iterable interface. Foreach assigns an iterator to these objects, and then calls hasNext () and Next () methods. You'd better use foreach to handle ArrayList objects, but for other collection objects, foreach is equivalent to using.

iterator

8. Avoid enumeration.

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

9. Declare external variables or methods that internal classes need to access within the scope of the package.

Java code:

public

Foo class

private int mValue

Public invalid operation () {

Inner in = new

inner();

mValue = 27

in . stuff();

}

Inside a private class {

void stuff() {

system . out . println(foo . this . mvalue);

}

}

}

//Foo$Inner is a completely independent class, and it is illegal to directly access the private members of Foo. The compiler will automatically generate a method:

/* package */static

int Foo . access $ 100(Foo Foo){

Return to foo.mValue

}

10, avoid using floating-point numbers.

Embedded processors usually have no hardware to support floating-point operations, and all operations on "floating-point" and "double precision" are realized by software.

We can avoid this problem by changing the declaration of variables and functions accessed by inner classes from private scope to package scope. Doing so 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 violates the classic OO principle. So you should use this optimization principle carefully when designing.