Android development of large bitmap two-time sample compression processing (source code sharing)

Source: Internet
Author: User

Pictures are available in various shapes and sizes. In many cases these pictures are much larger than our user interface (UI) and occupy a great deal of memory, and if we do not compress the bitmap, our program will have a memory leak error.

Mainactivity's Code

Package Com.example.g08_bitmap;import Android.os.bundle;import Android.app.activity;import Android.content.res.resources;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.widget.imageview;public class Mainactivity extends Activity {private ImageView ImageView; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Main); ImageView = (ImageView) This.findviewbyid (r.id.imageview1); Imageview.setimagebitmap ( Decodesampledbitmapfromresource (Getresources (), R.DRAWABLE.A, 300, 300));} public static Bitmap Decodesampledbitmapfromresource (Resources res,int resId, int reqwidth, int reqheight) {final BITMAPF Actory. Options options = new Bitmapfactory.options (),//First set Injustdecodebounds property to True, decoding avoids memory allocation options.injustdecodebounds = true;//the image into the selector Bitmapfactory.decoderesource (res, resId, options);//compress the picture at a specified scale options.insamplesize = Calculateinsamplesize (options, reqwidth,reqheight);//wait for the picture processing to be completed before the internalMemory leaks occur options.injustdecodebounds = False;return Bitmapfactory.decoderesource (res, resId, options);} Calculate the compressed proportions of a picture public static int calculateinsamplesize (bitmapfactory.options options,int reqwidth, int reqheight) {//Raw He ight and width of imagefinal int height = options.outheight;final int width = Options.outwidth;int Insamplesize = 1;if (He ight > Reqheight | | Width > Reqwidth) {final int heightratio = Math.Round ((float) height/(float) reqheight); final int widthRatio = math.ro und (float) width/(float) reqwidth);//Select a smaller ratio of length to width to become the compression ratio insamplesize = HeightRatio < WidthRatio? Heightratio:widthratio;} return insamplesize;}}


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.