Source analysis of "Listviewjson" "Com.demo.app.common" "Bitmapmanager" and its role in engineering

Source: Internet
Author: User

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

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.