Android tips-Optimization

Source: Internet
Author: User

Ii. Android Performance Optimization

1. Http uses gzip compression to set the connection timeout and response timeout

HTTP requests can be divided into cache and non-cache based on business needs. In a network-less environment, some data is still browsed through the cached httpresponse for offline reading.

2. listview Performance Optimization

1). Reuse convertview

In getitemview, determine whether convertview is empty. If it is not empty, it can be reused. If you need to add a listerner to the view in the couvertview,CodeIt must be outside of IF (convertview = NULL.

2) asynchronously load images

If the item contains a webimage, it is best to load it asynchronously.

3). The image is not displayed during fast sliding.

When the list is quickly swiped (scroll_state_fling), the image in the item or the view that consumes resources can be retrieved without being displayed. The image is in the other two States (scroll_state_idle and scroll_state_touch_scroll ), the view is displayed.

4). For images asynchronously loaded in the list, if they are not within the visible rangeAlgorithmTimely collection (for example, you can recycle up or down 10 items out of the current visible range, or cache the images and set a size. More than half will be recycled according to the latest minimum usage principle)

5). baseadapter to avoid memory overflow

If the entity class of baseadapter has attributes that consume a lot of memory, it can be saved to files. To improve performance, you can cache and limit the cache size.

3. the thread pool can be divided into the Core Thread Pool and general thread pool. time-consuming tasks such as image download are placed in the General thread pool to avoid waiting for all asynchronous tasks after time-consuming tasks block the thread pool.

4. asynchronous tasks are divided into core tasks and common tasks. Only system-level errors in core tasks report errors. The UI operation of asynchronous tasks needs to determine whether the original activity is active.

1) The main thread should not process the network;

2) The main thread should not process the database;

3) The main thread should not process files;

5. Avoid using static member variables to reference instances that consume too much resources, such as context

6. Use weakreference to replace strong references. Weak references allow you to keep references to objects, and allow GC to release objects and reclaim memory when necessary. For those objects that create cheap but consume a large amount of memory, that is, you want to keep this object, andProgramIf you want to recycle GC when necessary, you can use weak references.

7. Super fat bitmap

 
Timely destruction (bitmap is recycled when the ondestroy of the activity is used by the UI component. runtimeexception: canvas: trying to use a recycled bitmap Android. Graphics. Bitmap)
Set a certain sampling rate (sampling is not required for images provided by developers. For images uploaded by users or uncontrolled third parties, sampling can be performed to reduce the memory occupied by images ), returns the image from the server. We recommend that you provide the size of the image at the same time.
Clever Use of soft references
Drawable corresponds to resid resources, and bitmap corresponds to other resources
If an image of any type cannot be obtained (for example, the file does not exist or an outofmemory exception occurs when the file is read), there should be a corresponding default image (the default image is placed in the APK, obtained Through resid );

8. Ensure that the memory occupied by cursor is promptly released, rather than waiting for GC to process. In addition, Android obviously prefers programmers to manually close cursor.

9. threads are also an important source of Memory leakage. The main cause of thread Memory leakage is the uncontrollable thread lifecycle.

10. If the imageview image is from the network, load it asynchronously.

11. During custom views in application development, do not write the interaction part as a thread to constantly refresh the interface display. Instead, update the interface based on the touchlistener event.

12. drawable

The image used by the UI component is included in the APK package. Therefore, setimageresource or setbackgroundresource is used instead of resourceid.

Note: Get (getresources (), R. drawable. btn_achievement_normal) This method is converted to drawable through resid. You need to consider the recycling problem. If drawable is a private object of the object, the memory will not be released before the object is destroyed.

13. Reuse and Recycle activity objects

Temporary activity finish in time

Set the main interface to singletask.

Set the interface to singletop

14. Location Information

When obtaining the user's geographic location information, turn on the GPS when you need to obtain the data, and then close it in time

15. Set the power management for this interface during onresume and cancel the setting during onpause.

 

3. androidui Optimization

1. layout componentization, use merge and include reuse whenever possible

2. Use styles to reuse style definitions

3. the pop-up control of the soft keyboard should not overwrite the input box

4. Placement of numbers, letters, and Chinese characters: All digits and letters are divided into different types. In most cases, because our input is half-width, the placeholder values of letters and numbers cannot be determined. However, once fully divided, the placeholder values of numbers and letters are the same as those of a Chinese character, in this way, the layout problem caused by placeholder can be avoided.

5. English Document Layout: When textview is automatically changed, the word integrity should be maintained. The solution is to calculate the string length, and then manually set the number of letters displayed in each row with '\ n' added'

6. Use relativelayout for complex la s

7. adaptive screen; Use DP to replace pix

8. Use Android: layout_weight or tablelayout to create an equals Layout

9. Use animation-list to create an animation

 

Reprinted address:

Http://www.cnblogs.com/xiongbo/archive/2011/08/15/2098130.html

1). Reuse convertview

In getitemview, determine whether convertview is empty. If it is not empty, it can be reused. If you need to add listerner to the view in couvertview, the Code must be out of IF (convertview = NULL.

2) asynchronously load images

If the item contains a webimage, it is best to load it asynchronously.

3). The image is not displayed during fast sliding.

When the list is quickly swiped (scroll_state_fling), the image in the item or the view that consumes resources can be retrieved without being displayed. The image is in the other two States (scroll_state_idle and scroll_state_touch_scroll ), the view is displayed.

4 ). if the images asynchronously loaded in the list are not within the visible range, they are recycled in a timely manner according to certain algorithms (for example, the pictures outside the current visible range of 10 items are recycled, or cache the image, set a size, and recycle more than half according to the latest minimum usage principle)

5). baseadapter to avoid memory overflow

If the entity class of baseadapter has attributes that consume a lot of memory, it can be saved to files. To improve performance, you can cache and limit the cache size.

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.