Reference Links:
Displaying Bitmaps Efficiently
Caching Bitmaps Translation
First spit slot: Google to the example of the wrong place, the domestic translation does not remove the error. Too much pit.
Google official Download the sample
can also refer to Guolin's article,
After some study, the preliminary practice is completed. Put it out, spare.
Package Com.akm.testlrucache;import Android.app.activity;import Android.content.context;import Android.content.res.resources;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.os.asynctask;import Android.support.v4.util.lrucache;import Android.util.displaymetrics;import Android.view.window;import Android.view.windowmanager;import Android.widget.imageview;public class BitmapCacheUtil {private Context context;private int screenwidth = 0; private int screenheight = 0;private final int title_height = 48;pri vate ImageView imageview;private int resid;private lrucache<string, bitmap> Mmemorycache; Private Bitmapworkertask task; Public Bitmapcacheutil (context context, ImageView Imageview,int resid) {this.context = Context;this.imageview = Imagevie W;this.resid =resid; int maxmemory = (int) runtime.getruntime (). MaxMemory (); int cacheSize = MAXMEMORY/8; Mmemorycache = new lrucache<string, bitmap> (cacheSize) {@Override protected int sizeOf (STring Key, Bitmap Bitmap) {return bitmap.getbytecount (); } }; LoadBitmap ();} public void LoadBitmap () {Bitmap Bitmap = Getbitmapfrommemorycache (string.valueof (RESID)); if (bitmap==null) {//Mimagev Iew.setimageresource (r.drawable.image_placeholder); task = new Bitmapworkertask (); Task.execute (RESID); }else{imageview.setimagebitmap (bitmap); }} private Bitmap Getbitmapfrommemorycache (String key) {return mmemorycache.get (key); } private void Addbitmaptomemorycache (String key, Bitmap Bitmap) {if (Getbitmapfrommemorycache (key) = = null) {Mmemory Cache.put (key, bitmap); }} protected void Setimageview () {Bitmap Bitmap = Getbitmapfrommemorycache (string.valueof (resid)); if (bitmap! = null) {Imageview.setimagebitmap (bitmap); } else {Imageview.setimageresource (r.drawable.ic_launcher); }} class Bitmapworkertask extends Asynctask<integer, Void, bitmap> {@Override protected Bitmap Doinbackground (Integer ... params) {final Bitmap Bitmap = DECODESAMPLEDBITMAPFROmresource (Context.getresources (), Params[0], getscreenwidth (context), getscreenheight (context)); Addbitmaptomemorycache (String.valueof (Params[0]), bitmap); return bitmap;} @Override protected void OnPostExecute (Bitmap Bitmap) {super.onpostexecute (BITMAP); if (bitmap! = null) {Imageview.setimagebitmap (bitmap); }}} public Bitmap Decodesampledbitmapfromresource (Resources res, int resid,int reqwidth, int reqheight) {//First de Code with Injustdecodebounds=true to check dimensionsfinal bitmapfactory.options Options = new Bitmapfactory.options (); O Ptions.injustdecodebounds = true; Bitmapfactory.decoderesource (res, resId, options);//Calculate insamplesizeoptions.insamplesize = Calculateinsamplesize (Options, Reqwidth, reqheight);//Decode bitmap with insamplesize setoptions.injustdecodebounds = False;return Bitmapfactory.decoderesource (res, resId, options);} public int calculateinsamplesize (bitmapfactory.options Options, int reqwidth, int reqheight) {//Raw height and width of i Magefinal int height = options.outheight;final int width = Options.outwidth;int insamplesize = 1;if (height > Reqheight | | Width > Reqwidth) {if (width > height) {insamplesize = Math.Round ((float) height/(float) reqheight);} else {Insampl Esize = Math.Round ((float) width/(float) reqwidth);}} return insamplesize;} public void Canceltask () {if (task! = null) {Task.cancel (false); }} @SuppressWarnings ("deprecation") public int getscreenwidth (context context) {if (screenwidth! = 0) {return Screenwidt H;} WindowManager wm = (WindowManager) context.getsystemservice (context.window_service); screenwidth = Wm.getdefaultdisplay (). GetWidth (); return screenwidth;} @SuppressWarnings ("deprecation") public int getscreenheight (context context) {if (screenheight! = 0) {return ScreenHeight;} int top = 0;if (context instanceof Activity) {top = (activity) context). GetWindow (). Findviewbyid (window.id_android_ CONTENT). GetTop (); if (top = = 0) {top = (int) (Title_height * getscreendensity (context));}} WindowmAnager wm = (WindowManager) context.getsystemservice (context.window_service); screenheight = Wm.getDefaultDisplay (). GetHeight ()-Top;return screenheight;} Public float getscreendensity (context context) {WindowManager wm = (WindowManager) context.getsystemservice ( Context.window_service);D isplaymetrics metric = new Displaymetrics (); Wm.getdefaultdisplay (). Getmetrics (Metric); return metric.density;} Public float getscreendensitydpi (context context) {WindowManager wm = (WindowManager) context.getsystemservice ( Context.window_service);D isplaymetrics metric = new Displaymetrics (); Wm.getdefaultdisplay (). Getmetrics (Metric); return metric.densitydpi;}}
Android Caching Bitmaps Learning