First the source code is as follows:
PackageCom.demo.app.common;ImportJava.io.File;Importjava.io.IOException;Importjava.lang.ref.SoftReference;Importjava.util.Collections;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.WeakHashMap;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;ImportAndroid.graphics.Bitmap;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.widget.ImageView;Importcom.demo.app.AppException;Importcom.demo.app.api.ApiClient;/*** Async Line loads download picture Tool class * Instructions for use: * Bitmapmanager bmpmanager; * Bmpmanager = new Bitmapmanager (Bitmapfactory.decoderesource (conte Xt.getresources (), r.drawable.loading)); * BMPMANAGER.LOADBITMAP (ImageURL, ImageView); * @authorLiux (Http://my.oschina.net/liux) * @version1.0 * @created 2012-6-25*/ Public classBitmapmanager {Private StaticHashmap<string, softreference<bitmap>>Cache; Private StaticExecutorservice Pool; Private StaticMap<imageview, string>imageviews; PrivateBitmap defaultbmp; Static{Cache=NewHashmap<string, softreference<bitmap>>(); Pool= Executors.newfixedthreadpool (5);//Fixed thread poolImageviews = Collections.synchronizedmap (NewWeakhashmap<imageview, string>()); } PublicBitmapmanager () {} PublicBitmapmanager (Bitmap def) { This. defaultbmp =def; } /*** Set Default picture *@paramBMP*/ Public voidsetdefaultbmp (Bitmap bmp) {defaultbmp=bmp; } /*** Load Picture *@paramURL *@paramImageView*/ Public voidloadbitmap (String URL, ImageView ImageView) {loadbitmap (URL, ImageView, This. defaultbmp, 0, 0); } /*** Load picture-can set the default picture displayed after loading failed *@paramURL *@paramImageView *@paramdefaultbmp*/ Public voidloadbitmap (String URL, ImageView ImageView, Bitmap defaultbmp) {loadbitmap (URL, ImageView, defaultbmp, 0, 0); } /*** Load Picture-You can specify the height of the display image *@paramURL *@paramImageView *@paramWidth *@paramHeight*/ Public voidLoadBitmap (String URL, ImageView ImageView, Bitmap defaultbmp,intWidthintheight) {imageviews.put (ImageView, URL); Bitmap Bitmap=getbitmapfromcache (URL); if(Bitmap! =NULL) { //Show Cached PicturesImageview.setimagebitmap (bitmap); } Else { //load the image cache from the SD cardString filename =fileutils.getfilename (URL); String filepath= Imageview.getcontext (). Getfilesdir () + File.separator +filename; File File=NewFile (filepath); if(File.exists ()) {//Display the image cache on the SD cardBitmap BMP =Imageutils.getbitmap (Imageview.getcontext (), filename); Imageview.setimagebitmap (BMP); }Else{ //line loads load network picturesImageview.setimagebitmap (defaultbmp); Queuejob (URL, imageView, width, height); } } } /*** Get pictures from the cache *@paramURL*/ PublicBitmap getbitmapfromcache (String url) {Bitmap Bitmap=NULL; if(Cache.containskey (URL)) {bitmap=cache.get (URL). get (); } returnbitmap; } /*** Load images from the network *@paramURL *@paramImageView *@paramWidth *@paramHeight*/ Public voidQueuejob (FinalString URL,FinalImageView ImageView,Final intWidthFinal intheight) { /*Create handler in UI thread.*/ FinalHandler Handler =NewHandler () { Public voidhandlemessage (Message msg) {String tag=Imageviews.get (ImageView); if(Tag! =NULL&&tag.equals (URL)) { if(Msg.obj! =NULL) {imageview.setimagebitmap (Bitmap) msg.obj); Try { //write the picture cache to the SD cardImageutils.saveimage (Imageview.getcontext (), Fileutils.getfilename (URL), (Bitmap) msg.obj) ; } Catch(IOException e) {e.printstacktrace (); } } } } }; Pool.execute (NewRunnable () { Public voidrun () {Message message=Message.obtain (); Message.obj=downloadbitmap (url, width, height); Handler.sendmessage (message); } }); } /*** Download picture-can specify the height of the display image *@paramURL *@paramWidth *@paramHeight*/ PrivateBitmap downloadbitmap (String URL,intWidthintheight) {Bitmap Bitmap=NULL; Try { //http Loading PicturesBitmap =apiclient.getnetbitmap (URL); if(Width > 0 && height > 0) { //Specifies the height of the display pictureBitmap = Bitmap.createscaledbitmap (bitmap, width, height,true); } //put CacheCache.put (URL,NewSoftreference<bitmap>(bitmap)); } Catch(appexception e) {e.printstacktrace (); } returnbitmap; } }
Source analysis of "Listviewjson" "Com.demo.app.common" "Bitmapmanager" and its role in engineering