Anyone who learns Android should know that Glide is an incredibly powerful image loading library that has a good caching mechanism in it for us to choose from, and we only need one parameter call (Diskcachestrategy ()), Without having to implement it yourself like the Universal-image-loader class. It is because it is so useful that it makes it easy to overlook some important details. Android's Bitmap object is one of the most likely culprits for oom, and if we load a lot of bitmap objects, we have to prevent oom.
My intention is to load the picture into the Recyclerview. In the Staggeredgridlayoutmanager Mode 2 column display, realize the waterfall flow effect
Mrecyclerview.setlayoutmanager (new Staggeredgridlayoutmanager (2, staggeredgridlayoutmanager.vertical));
But because the picture is obtained in the network, do not know its specific size, so start with target to achieve, specifically as follows:
Glide.with (Itemview.getcontext ()). Load (URL). Asbitmap (). Placeholder (r.drawable.error_pic). DiskCache Strategy (Diskcachestrategy.all). into (NewSimpletarget<bitmap>(target.size_original, target.size_original) {@Override Public voidOnresourceready (Bitmap resource, glideanimation<? Super Bitmap>glideanimation) { //Original picture Width height intImageWidth =resource.getwidth (); intImageHeight =resource.getheight (); //shrink a picture proportionally floatRatio= (float) ((imagewidth*1.0)/(width*1.0)); intHeight= (int) (imageheight*1.0/ratio); Viewgroup.layoutparamsparams=Ivimage.getlayoutparams (); params. width=width; params. height=height; Ivimage.setimagebitmap (Resource); } });
And before I did this, I was able to load it, but it was loaded with some dozens of KB of small pictures, and there was no problem with Oom. But in the project I'm doing now, because the source image of the picture that needs to be loaded is MB, it can't be loaded into the bitmap as before.
Later remembered before useless glide before a method can not need to load the picture into the bitmap can also get to the width of the picture, then think about whether it can be combined with the use. Here's how to get the picture height without loading the picture:
//get the picture size without loading the picture Public Static int[] getimagewidthheight (String path) {bitmapfactory.options Options=Newbitmapfactory.options (); /** * Most critical here, put options.injustdecodebounds = true; * Here again DecodeFile (), return the bitmap is empty, but at this time call Options.outheight, already contains the picture of the high*/Options.injustdecodebounds=true; Bitmap Bitmap= Bitmapfactory.decodefile (path, options);//the bitmap returned at this time is null /** *options.outheight for the original picture of the high*/ return New int[]{options.outwidth,options.outheight};}
So I get the width of the original image, then I do not need to use the target object to get the picture, but instead of directly re-set the image width, but also before the picture, such as scaling:
//Get screen widthDisplaymetrics Outmetrics =NewDisplaymetrics (); WindowManager Manager=Getwindowmanager (); Manager.getdefaultdisplay (). Getmetrics (outmetrics); width= outmetrics.widthpixels/2;//scale by width, or you'll get Oomint[] width_height= Filehelper.getimagewidthheight (neturl.dir+"/"+data);floatRatio= (float) ((width_height[0]*1.0)/(width*1.0));intHeight= (int) (width_height[1]*1.0/ratio);
Finally, call glide directly to reset the size:
Glide.with (Itemview.getcontext ()). load (URL). asbitmap () . Placeholder (r.drawable.error_pic) . Diskcachestrategy (Diskcachestrategy.result) . Override (Width,height)
Android Recyclerview using glide to load a large number of images into (Target) cause Oom exception