Android Image code compression (effect is simply exploding)

Source: Internet
Author: User

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)

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.