Tens of thousands of daily launches to prove that the load is good;
There are a lot of third-party loading frameworks, and it is recommended to use mature frameworks, such as imageload, which we all know,
Here's just for learning;
I also in a menu project to write overload into the mobile phone album picture, but it was not good at that time, I will write another, this project with this jar, tens of thousands of loading occasionally see Samsung's newspaper two or three oom okay
Cache Management is here
http://blog.csdn.net/intbird/article/details/38338713
Picture processing in here
http://blog.csdn.net/intbird/article/details/38338811
Interface Recycling Pictures here
http://blog.csdn.net/intbird/article/details/19905549
The tool jar package is explained here:
Http://pan.baidu.com/s/1dDtOZbr
An integrated [cache and picture Resource loading] tool class, Bitmaphelper,cachehelper,
Http://pan.baidu.com/s/1dDtOZbr
Usage://Note that the ANDROID-SUPPORT-V4 package must be imported.
1, initialize the cache folder when the app is initialized:
(SD home directory, photo directory, image cache directory)
The home folder is generally the appname in the application's GetString ();
Cachehelper.initcache ("Intbird");
Some methods that are used frequently
2, initialize Picture helper class
Bitmaphelper bitmaphelper=new Bitmaphelper (context,r.drawable.ic_launcher,r.drawable.ic_launcher);
Bitmaphelper bitmaphelper=new bitmaphelper (context);
Finally loaded into the implementation class
Bitmaphelper.loadoptions options=new bitmaphelper.loadoptions ();
Some simple picture processing of the package, in other used places in the description
3 External helper classes, where pictures are encountered in the encapsulation program
public static class Imageoptions implements serializable{public int imageType; public int cropwidth; public int cropheight; public int displaywidth; public int displayheight; public int qulity; public int maxkb=-1; public boolean isneedroteange=false; public boolean istumbnail; Public Imageuploadtype Uploadtype; Public Imagecroptype Croptype; Known loading method public loadbitmapfinish finish; Public displayimageoptions displayoption; public Imageloadinglistener Listener; }
4, general quick record single picture
Bitmaphelper.loadbitmap (Bitmaphelper.img_type_file, FILEURL, Iv,options.displaywidth,options.displayheight, Options.finish);
The internal real implementation, can construct this many other functions
Bitmaphelper.loadbitmap (loadoption);
Finish
Loadbitmapfinish returns after failure and success, bitmap returns empty when failed
5, for list loading implementations
In front of the load, add a True,means is used in the ListView;
Bitmap.loadbitmap (True,bitmaphelper.img_type_web, value, v,200,200,null);
The internal real implementation, can construct this many other functions
Bitmaphelper.loadbitmap (loadoption);
6, Demo sample
Single
Bitmaphelper.loadbitmap (Bitmaphelper.img_type_file, FILEURL, Iv,options.displaywidth,options.displayheight, Options.finish);
List
@Overridepublic void Setviewimage (ImageView V, String value) {<span style= "White-space:pre" ></span> Bitmap.loadbitmap (True,bitmaphelper.img_type_web, value, V,200,200,null);}
//Stop loadingBitmaphelper.finishloadsingle ();//single
adapter.finishload ();//List
public void Finishload () {
if (bitmap!=null)
Bitmap.finishloadforlist ();
}
7, loading effect
1.0.0 Approximate implementation: The version number has been changed several times and the error messages collected by different phones in the project have been changed, please use the latest version number on the cloud disk to ensure that the project is working properly ( The latest is what I used in the project), the code below is for learning only ....
/** * @author [email protected] * @time 20140606 * * Package Com.intbird.utils;import Java.lang.ref.weakreference;i Mport Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmap.config;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.paint;import Android.graphics.porterduff.mode;import Android.graphics.porterduffxfermode;import Android.graphics.Rect;import Android.graphics.drawable.bitmapdrawable;import Android.graphics.drawable.drawable;import Android.media.thumbnailutils;import Android.os.asynctask;import Android.widget.imageview;public class BitmapHelper {Private CacheManager cachemanager;public static final int img_type_res=0;public static final int img_type_web=1;public s tatic Final int img_type_file=3;private Bitmap bmpholder=null;private Bitmap bmpnoimg=null;public bitmaphelper (Context Context,int loadingid,int loadnoid) {cachemanager=cachemanager.getinstance (); bmpholder= Bitmapfactory.decoderesource (cOntext.getresources (), loadingid); Bmpnoimg=bitmapfactory.decoderesource (Context.getresources (), loadNoId);} /** * Load Picture * @param type network file, local file, or resource file * @param fileUrl URL * @param imageView control * @param width to get the specified height * @param heig HT to get the specified width */public void commonloadbitmap (int type,string fileurl,imageview imageview,int width,int height) {// There is no picture in the memory and the file, once again gets; Bitmap Bmp=cachemanager.getbitmapfromcache (FILEURL); if (bmp!=null) {imageview.setimagebitmap (BMP);} Else{switch (type) {case Img_type_web:loadmultibitmapfromweb (FILEURL, imageview,width,height); Break;case IMG_TYPE_ FILE:imageView.setImageBitmap (Getbitmapformfile (fileUrl, width, height, true)); Break;case Img_type_res: Imageview.setimageresource (Integer.parseint (FILEURL)); /** * Set ImageView to get the specified width of the specified file address of the image specified height; * @param fileUrl file address * @param reqwidth target width * @param reqheight target Height * @return Bitmap */public bitmap loadsinglebitmapfrom File (String fileurl,imageview iv,int reqwidth,int reqheight) {bitmapfactory.options options=new BitmapFactorY.options (); Options.injustdecodebounds=true;options.insamplesize=calculateinsamplesize (Options, ReqWidth, Reqheight); Bitmapfactory.decodefile (fileurl,options); options.injustdecodebounds=false; Bitmap Bmp=bitmapfactory.decodefile (fileurl,options); if (iv!=null) iv.setimagebitmap (BMP); return bmp;} /** * Set ImageView as a web image that needs to be loaded; * @param WEBURL * @param imageView * @param width * @param heigth */public void Loadsinglebitmapfromweb (String weburl,imag Eview imageview,int width,int heigth) {bitmaphelper.bitmapworkersingletask task=new BitmapWorkerSingleTask (imageView ); Task.execute (weburl,width+ "", heigth+ "");} /** * Adapter Loading picture * @param fileUrl image address * @param imageView picture control * @param width to get the height of @param height */public voi D loadmultibitmapfromweb (String fileUrl, ImageView imageview,int width,int height) {if (Cancelpotentialwork (FILEURL, I Mageview) {Final Bitmapmultiworkertask task = new Bitmapmultiworkertask (ImageView); Final asyncdrawable asyncdrawable =new ASyncdrawable (Bmpholder, Task); Imageview.setimagedrawable (asyncdrawable); Task.execute (fileurl,width+ "", height+ ""); }}/** * Load a required bitmap from the network; * @param WEBURL * @param reqwidth * @param reqheight * @return */public Bitmap getbitmapformweb (String weburl,int reqwidth , int reqheight) {bitmapfactory.options options=new bitmapfactory.options (); Options.injustdecodebounds=true;o Ptions.insamplesize=calculateinsamplesize (Options, Reqwidth, reqheight); return Conninternet.loadbitmapfromnet ( WEBURL, options);} /** * Convert picture file to bitmap * * @param fileUrl * picture file path * @param width * Width * @param height * high Degrees * @param isthumbnail * Whether to generate thumbnails based on aspect * @return */public Bitmap getbitmapformfile (String fileUrl, int width, int Height,boolean isthumbnail) {Bitmap bitmap=loadsinglebitmapfromfile (fileurl,null, width, height);//Generate a fixed-size thumbnail if ( Isthumbnail) {bitmap = Thumbnailutils.extractthumbnail (bitmap, Width, height,thumbnailutils.options_recycle_INPUT);} Cachemanager.addbitmaptocache (FileUrl, bitmap); return bitmap;} /** * Convert picture into circle * * @param bitmap * @return */public static bitmap Changebitmaptoround (bitmap bitmap) {if (bitmap==null) RET Urn Null;int width = bitmap.getwidth (); int height = bitmap.getheight (); float roundpx;float left, top, right, bottom, dst_l EFT, Dst_top, Dst_right, dst_bottom;if (width <= height) {roundpx = Width/2;left = 0;top = 0;right = Width;bottom = W Idth;height = Width;dst_left = 0;dst_top = 0;dst_right = Width;dst_bottom = width;} else {roundpx = height/2;float clip = (width-height)/2;left = Clip;right = Width-clip;top = 0;bottom = Height;widt H = height;dst_left = 0;dst_top = 0;dst_right = Height;dst_bottom = height;} Bitmap output = bitmap.createbitmap (width, height, config.argb_8888); Canvas canvas = new canvas (output), final int color = 0xff424242;final Paint paint = new paint (); final rect src = new rect ( (int) left, (int) top, (int.) right, (int) bottom), final rect dst = new rect ((int) Dst_left,(int) dst_top, (int) dst_right, (int) dst_bottom);p Aint.setantialias (TRUE);//Set Brush non-aliased canvas.drawargb (0, 0, 0, 0); Fills the entire canvaspaint.setcolor (color);//There are two ways to draw circles, Drawrounrect and drawcircle//canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint);////draws rounded rectangles, the first of which is a graphical display area. The second and third parameters respectively are the horizontal fillet radius and the vertical fillet radius. Canvas.drawcircle (ROUNDPX, ROUNDPX, ROUNDPX, paint);p Aint.setxfermode (New Porterduffxfermode (mode.src_in));// Set the pattern at which two pictures intersect, refer to Http://trylovecatch.iteye.com/blog/1189452canvas.drawBitmap (bitmap, SRC, DST, paint); Merge bitmap and Circlereturn output with draw in mode.src_in mode;} /** * Calculate the required size of the picture compression ratio * @param options * @param reqwidth required Width * @param reqheight Required Height * @return Compression ratio */public int Calcula Teinsamplesize (bitmapfactory.options Options, int reqwidth, int reqheight) {final int height = options.outheight; Final int width = options.outwidth; int insamplesize = 1; if (Height > Reqheight | | width > reqwidth) {final int halfheight = HEIGHT/2; Final int halfwidth = WIDTH/2; while ((halfheight/insamplesize) > Reqheight && (halfwidth/insamplesize) > Reqwidth) {i Nsamplesize *= 2; }}return Insamplesize;} /** * Check startup Task * @param fileUrl file path * @param imageView Target Control * @return whether to start loading task */private Boolean cancelpotentialwork (stri Ng FileUrl, ImageView ImageView) {final Bitmapmultiworkertask bitmapworkertask = Getbitmapworkertask (ImageView); if (bitmapworkertask! = null) {String bitmapurl = Bitmapworkertask.fileurl; if (bitmapurl!=null&&bitmapurl! = fileUrl) {Bitmapworkertask.cancel (true); return true; } else {return false; }} return true; /** * Gets the load task in the picture control * @param ImageView * @return Returns the task */private Bitmapmultiworkertask getbitmapworkertask (ImageView imag Eview) {if (ImageView! = null) {final drawable drawable = imageview.getdrawable (); if (drawable instanceof asyncdrawable) {Final asYncdrawable asyncdrawable = (asyncdrawable) drawable; return Asyncdrawable.getbitmapworkertask (); }} return null;} Private class Asyncdrawable extends Bitmapdrawable {private final weakreference<bitmapmultiworkertask> Bitmapwor Kertaskreference; @SuppressWarnings ("deprecation") public asyncdrawable (Bitmap bitmap,bitmapmultiworkertask bitmapworkertask) {super ( Bitmap); Bitmapworkertaskreference =new weakreference<bitmapmultiworkertask> (bitmapworkertask); } public Bitmapmultiworkertask Getbitmapworkertask () {return bitmapworkertaskreference.get (); }}private class Bitmapmultiworkertask extends Asynctask<string, Void, bitmap> {private weakreference< imageview> imageviewreference;private String fileurl;public bitmapmultiworkertask (ImageView ImageView) { Imageviewreference=new weakreference<imageview> (ImageView);} @Overrideprotected Bitmap doinbackground (String ... params) {Fileurl=params[0];int Reqwidth=integEr.valueof (params[1]); int reqheight=integer.valueof (params[2]); Bitmap Bitmap=getbitmapformweb (fileurl,reqwidth,reqheight); return Bitmap;} @Override protected void OnPostExecute (Bitmap Bitmap) {if (iscancelled ()) {Bitmap = null; } if (imageviewreference! = null) {final ImageView ImageView = Imageviewreference.get (); Final Bitmapmultiworkertask bitmapworkertask =getbitmapworkertask (ImageView); if (this = = Bitmapworkertask && ImageView! = null) {if (bitmap!=null) {Imagevie W.setimagebitmap (bitmap); Cachemanager.addbitmaptocache (FileUrl, bitmap); } else{Imageview.setimagebitmap (bmpnoimg); }}}}}/** * Asynchronously loads a picture * @author [email protected] * */private class Bitmapworkersingletask Exte NDS asynctask<string, Void, Bitmap>{private Weakreference<imageview> imageviewreference;private String fileurl= "";p ublic bitmapworkersingletask (ImageView ImageView) { Imageviewreference=new weakreference<imageview> (ImageView);} @Overrideprotected Bitmap doinbackground (String ... params) {Fileurl=params[0];int reqwidth=integer.valueof (params[1 ]); int reqheight=integer.valueof (params[2]); Bitmap Bitmap=getbitmapformweb (fileurl,reqwidth,reqheight); return Bitmap;} @Overrideprotected void OnPostExecute (Bitmap Bitmap) {if (imageviewreference! = null) {final ImageView Imagev Iew = Imageviewreference.get (); if (ImageView! = null) {if (bitmap!=null) {imageview.setimagebitmap (bitmap); } else{Imageview.setimagebitmap (bmpnoimg); } } }}}}
Android comes with the processing of bitmap out Memory, I just changed some of the things I used to make myself.