Description: The picture is not cached, but the bitmap resource is freed with a soft reference to avoid a memory leak.
To decode a bitmap display:
1 PublicBitmap Decodebitmap (Resources resources,intResId,intReqwith, Reqheight) {2 //parameter settings for decoding a bitmap3Bitmapfactory.options Options =Newbitmapfactory.options ();4 //avoid requesting memory space during decoding of bitmaps5Options.injustdecodebounds =true;6 Bimapfactory.decoderesource (Resources, resId, options);7 //compress the image in a certain proportion8Options.insamplesize =caculateinsimplesize (Options, Reqwith, reqheight);9 //true Output bitmapTenOptions.injustdecodebounds =false; One returnBimapfactory.decoderesource (Resources, resId, options); A}
Calculate picture Compression Scale:
Public intCaculateinsimplesize (Bitmapfactory.options Options,intReqwidth,intreqheight) { // intImageHeight =Options.outheight; intImageWidth =Options.outwidth; intInsimpesize = 1;//Compression ratio if(ImageHeight > Reqheight | | imagewidth >reqwidth) { Final intHeightRatio = Math.Round ((float) ImageHeight/(float) reqheight); Final intWidthRatio = Math.Round ((float) ImageWidth/(float) reqwidth); Insimplesize= HeightRatio < WidthRatio?Heightratio:widthratio; } returninsimplesize;}
Network Picture request:
1 Public Static byte[] sendpost (String path) {2HttpClient HttpClient =Newdefaulthttpclient ();3HttpPost HttpPost =Newhttpost (path);4HttpResponse response =NULL;5 Try {6Response =Httpclient.execute (httppost);7 if(Response.getstatusline (). Getstatuscode () = = 200) {8 returnEntityutils.tobytearray (Response.getentity ());9 }Ten}Catch(Exception e) { One e.printstacktrace (); A}finally { - Httpclient.getconnectionmanager (). shutdown (); - } the return NULL; -}
Bulk load Large bitmaps:
1 //processing the display in GetView in Adpter2 PublicView GetView (intposition, View Converview, ViewGroup parent) {3View view =NULL;4 if(Converview = =NULL ){5View = Layoutinflater.from (mainactivity. This,). Inflate (R.layout.item,NULL);6}Else {7View =Converview;8 }9ImageView ImageView =(ImageView) View.findviewbyid (r.id.item_image);Ten //Get Pictures One LoadBitmap (path[position], imageView); A returnview; -}
//when you slide the listview, the old layout is recycled, and if the ListView is combined with an asynchronous task operation, you cannot ensure that the reused layout is reclaimed in a timely manner. Static classAsyncdrawableextendsbitmapdrawable{Private FinalSoftreference<bitmapworkertask>SoftReference; Publicasyncdrawable (Resources resources, Bitmap Bitmap, Bitmapworkertask bitmapworkertask) {Super(resources, bitmap); SoftReference= softreference<mainactivity.bitmapworkertask>(Bitmapworkertask); } PublicBitmapworkertask Getbitmapworkertask () {returnSoftreference.get (); }}/**Asynchronous Task **/classBitmapworkertaskextendsAsynctask<string,void, bitmap> { PrivateSoftreference<imageview>imagesoftreference; PrivateString data = ""; PublicBitmapworkertask (ImageView ImageView) {imagesoftreference=NewSoftreference<imageview>(ImageView); } @OverrideprotectedBitmap doinbackground (String ... params) {data= Params[0]; byte[] result =sendpost (data); //decoding the bitmap, returning 100*100 returnDecodebitmap (result, 100, 100); } @Override potectedvoidOnPostExecute (Bitmap Bitmap) {Super. OnPostExecute (bitmap); if(iscancelled ()) {bitmap=NULL; } if(Imagesoftreference! =NULL&& Bitmap! =NULL) { FinalImageView ImageView =Imagesoftreference.get (); FinalBitmapworkertask Bitmapworkertask =Getbitmapworkertask (ImageView); //loading an asynchronous task is a separate thread, which guarantees that reference ImageView and asynchronous tasks are associated. if( This= = Bitmapworkertask && ImageView! =NULL) {imageview.setimagebitmap (bitmap); } } }}Private Staticbitmapworkertask getbitmapworkertask (ImageView ImageView) {if(ImageView! =NULL) { FinalDrawable drawable =imageview.getdrawable (); if(drawableinstanceofasyncdrawable) { FinalAsyncdrawable asyncdrawable =(asyncdrawable) drawable; returnAsyncdrawable.getbitmapworkertask (); } } return NULL;}//checks if there is another asynchronous task that executes and imageview to bind, and the previous asynchronous task cancels the operation Public Static Booleancancelpotntialwork (String data, ImageView ImageView) {FinalBitmapworkertask Bitmapworkertask =Getbitmapworkertask (ImageView); if(Bitmapworkertask! =NULL) { FinalString BitmapData =Bitmapworkertask.data; if(BitmapData! =data) {Bitmapworkertask.cancel (true); } Else { return false; } } return true;}//Loading Pictures Public voidLoadBitmap (String data, ImageView ImageView) {Bitmap Placebitmap=Bitmapfactory.decoderesource (Getresources (), R.drawable.empty_photo); if(cancelpotntialwork (data, ImageView)) {FinalBitmapworkertask task =NewBitmapworkertask (ImageView); FinalAsyncdrawable asyncdrawable =Newasyncdrawable (Getresources (), Placebitmap, Task); Imageview.setimagedrawable (asyncdrawable); Task.execute (data); }}
Android Large Bitmap Loading