Go Android Load picture Stack Overflow

Source: Internet
Author: User

1. Loading thumbnails

/*** Load pictures by path *@paramPath Picture resource storage paths *@paramscalsize Reduction in multiples *@return*/ Public StaticBitmap Loadresbitmap (String path,intscalsize) {bitmapfactory.options Options=Newbitmapfactory.options (); Options.injustdecodebounds=false; Options.insamplesize=scalsize; Bitmap BMP=bitmapfactory.decodefile (path, options); returnbmp;}

This method does not occupy a lot of memory, but its fatal disadvantage is that because the load is the thumbnail, so the image distortion is more serious, for the image quality requirements of high application.

2. Use Java Soft Reference, the image cache, will often need to load the picture, stored in the cache, to avoid repeated loading.

For a detailed description of soft references (softreference), see http://www.auyou.cn/club/clubbbsinfo-9255.html. The following is an image caching tool class for the original author.

/**** @authorlarson.liu* This class is used for picture caching to prevent memory overflow*/ Public classBitmapcache {Static*Bitmapcache Cache;/**Storage for Chche content*/*Hashtable bitmaprefs;/**Garbage Reference Queue (the referenced object has been reclaimed, then the reference is put into a queue)*/*Referencequeue Q;/*** Inherit softreference so that each instance has an identifiable identity. */*classBtimaprefextendsSoftReference {* Integer _key = 0;  PublicBtimapref (Bitmap bmp, Referencequeue Q,intkey) {Super(BMP, Q); _key=key;}} *Bitmapcache () {bitmaprefs=NewHashtable (); Q=Newreferencequeue ();} /*** Get Cache instance*/ Public StaticBitmapcache getinstance () {if(Cache = =NULL) {Cache=NewBitmapcache ();}returnCache;} /*** Refer to an instance of a bitmap object in a soft-reference manner and save the reference*/*voidAddcachebitmap (Bitmap bmp, Integer key) {Cleancache ();//Clear Junk ReferencesBtimapref ref =Newbtimapref (BMP, Q, key); Bitmaprefs.put (key, ref);} /*** Retrieve an instance of the corresponding bitmap object based on the image resource ID number under the specified drawable (can be obtained from the network or local path, depending on your needs)*/ PublicBitmap Getbitmap (intResId, Context context) {Bitmap BMP=NULL;//whether there is a soft reference to the bitmap instance in the cache, if any, from a soft reference. if(Bitmaprefs.containskey (resId)) {Btimapref ref=(btimapref) bitmaprefs.get (resId); BMP=(Bitmap) Ref.get ();}//If there is no soft reference, or if the instance obtained from the soft reference is NULL, an instance is rebuilt,//and save a soft reference to the new instanceif(BMP = =NULL) {BMP=Bitmapfactory.decoderesource (Context.getresources (), resId); This. Addcachebitmap (BMP, ResId);}returnbmp;} *voidCleancache () {btimapref ref=NULL; while(ref = (BTIMAPREF) q.poll ())! =NULL) {bitmaprefs.remove (Ref._key);}} //clear all content in cache Public voidClearCache () {Cleancache (); Bitmaprefs.clear (); System.GC (); System.runfinalization ();} }

Call the class in the program code:

this));

This way, when your imageview needs to change the background image back and forth, it doesn't need to be loaded again.

Method Three:

Destroy bitmap objects that are no longer in use in a timely manner.

if null && b!  null//  Recycle () is a lengthy process, set to NULL, and then the last call to System.GC (), the effect is much better }
System.GC ();

Go Android Load picture Stack Overflow

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.