Words do not say much, directly on the code, the main function directly godless two methods in, copy and paste can
//image compression for aspect ratios Public Static intcalculateinsamplesize (bitmapfactory.options Options,intReqwidth,intreqheight) { //the height and width of the source picture Final intHeight =Options.outheight; Final intwidth =Options.outwidth; intInsamplesize = 1; if(Height > Reqheight | | width >reqwidth) { //calculates the ratio between the actual width and the target width Final intHeightRatio = Math.Round ((float) Height/(float) reqheight); Final intWidthRatio = Math.Round ((float) Width/(float) reqwidth); //Select width and high school minimum ratio as the value of the insamplesize, so that the final picture width and height can be guaranteed//must be greater than or equal to the width and height of the target. Insamplesize = HeightRatio < WidthRatio?Heightratio:widthratio; } returninsamplesize; } //image compression Function Public StaticBitmap Decodesampledbitmapfromresource (Resources res,intResId,intReqwidth,intreqheight) { //The first resolution sets Injustdecodebounds to true to get the picture size FinalBitmapfactory.options Options =Newbitmapfactory.options (); Options.injustdecodebounds=true; Bitmapfactory.decoderesource (res, resId, options); //call the method defined above to calculate the Insamplesize valueOptions.insamplesize =calculateinsamplesize (Options, Reqwidth, reqheight); //parse the picture again using the obtained insamplesize valueOptions.injustdecodebounds =false; returnBitmapfactory.decoderesource (res, resId, options); }
Method call, where the picture is to plug where, directly to the picture to you 100*100
Imageview.setimagebitmap ( 100, 100));
Android Image code compression (effect is simply exploding)