Android advanced: Performance Optimization

Source: Internet
Author: User

1. When using the gallery control, if too many images are loaded and too large, an outofmemoryerror exception may easily occur, that is, memory overflow. This is because Android only allocates a few MB of memory by default, and if the uploaded image is in JPG or another compression format, it will occupy a large amount of space when it is expanded in the memory, thus easily causing memory overflow. In this case, you can use the following method to solve the problem:

 

Imageview I = new imageview (mcontext); <br/> bitmapfactory. options = new bitmapfactory. options (); <br/> options. insamplesize = 10; <br/> // it seems that the options function is to return a thumbnail. 10 indicates that the length and width are 1/10 of the original width, that is, the original area is 1/100 <br/> // The thumbnail can reduce memory usage <br/> bitmap Bm = bitmapfactory. decodefile (LIS. <br/> get (position ). tostring (), options); <br/> I. setimagebitmap (BM); <br/> BM. recycle (); <br/> // reclaim Resources

 

Ii. unified management of Bitmap resources and timely release of resources

 

Class imagemanager {<br/> private weakhashmap <integer, weakreference <bitmap> mbitmaps; <br/> private weakhashmap <integer, weakreference <drawable> mdrawables; </P> <p> private Boolean mactive = true; </P> <p> Public imagemanager () {<br/> mbitmaps = new weakhashmap <integer, weakreference <bitmap> (); <br/> mdrawables = new weakhashmap <integer, weakreference <drawable> (); <br/>}</P> <p> Public bitma P getbitmap (INT Resource) {<br/> If (mactive) {<br/> If (! Mbitmaps. containskey (Resource) {<br/> mbitmaps. put (resource, <br/> New weakreference <bitmap> (bitmapfactory. decoderesource (mainactivity. getcontext (). getresources (), Resource); <br/>}< br/> return (weakreference <bitmap>) mbitmaps. get (Resource )). get (); <br/>}< br/> return NULL; <br/>}</P> <p> Public drawable getdrawable (INT Resource) {<br/> If (mactive) {<br/> If (! Mdrawables. containskey (Resource) {<br/> mdrawables. put (resource, new weakreference <drawable> (getapplication (). getresources (). getdrawable (Resource); <br/>}< br/> return (weakreference <drawable>) mdrawables. get (Resource )). get (); <br/>}< br/> return NULL; <br/>}</P> <p> Public void recyclebitmaps () {<br/> iterator itr = mbitmaps. entryset (). iterator (); <br/> while (itr. hasnext () {<br/> map. entry E = (map. entry) itr. next (); <br/> (weakreference <bitmap>) E. getvalue ()). get (). recycle (); <br/>}< br/> mbitmaps. clear (); <br/>}</P> <p> Public imagemanager setactive (Boolean B) {<br/> mactive = B; <br/> return this; <br/>}</P> <p> Public Boolean isactive () {<br/> return mactive; <br/>}< br/>}

 

3. Network Connections often consume a large amount of energy.

In the program that requires network connection, we can first check whether the network connection is normal. If there is no network connection, we do not need to execute the corresponding program.



To check the network connection, follow these steps:

 

Private Boolean isconnected () {<br/> connectivitymanager mconnectivity = (connectivitymanager) This. getsystemservice (connectivity_service); <br/> telephonymanager mtelephony = (telephonymanager) getsystemservice (context. telephony_service); </P> <p> // check the network connection. If no network is available, you do not need to perform network connection operations. <br/> networkinfo info = mconnectivity. getactivenetworkinfo (); <br/> If (Info = NULL | <br/>! Mconnectivity. getbackgrounddatasetting () {<br/> return false; <br/>}< br/> // determines the network connection type. Only 3G or Wi-Fi data is updated. <Br/> int nettype = info. getType (); <br/> int netsubtype = info. getsubtype (); <br/> If (nettype = connectivitymanager. type_wifi) {<br/> return info. isconnected (); <br/>} else if (nettype = connectivitymanager. type_mobile <br/> & netsubtype = telephonymanager. network_type_umts <br/> &&! Mtelephony. isnetworkroaming () {<br/> return info. isconnected (); <br/>}else {<br/> return false; <br/>}< br/>}

 

4. resource-consuming data transmission between networks, including transmission and Resolution Methods

 

View a table

 

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.