How Android scales and rotates Images

Source: Internet
Author: User

In Android Application Development, there will be many places where image scaling is required, either out of appearance or out of memory savings ..

Here, the author summarizes several common image scaling methods.

First: Use bitmapfactory. Options

Bitmapfactory. options opts = new bitmapfactory. options (); opts. injustdecodebounds = true; bitmapfactory. decodefile (picpathstring, opts); // only load the boundary information of the image to the memory, saving the memory log. I (TAG, "opts. outheight ======> "+ opts. outheight); log. I (TAG, "opts. outwidth ====> "+ opts. outwidth); If (OPTs. outheight! = 0 & opts. outwidth! = 0) {opts. insamplesize = (INT) (OPTs. outwidth * 5)/width); // The insamplesize parameter is a compression multiple. you can adjust it by yourself. It must be an int log. I (TAG, "opt. insamplesize ----> "+ opts. insamplesize);} opts. injustdecodebounds = false; Bitmap bitmap = bitmapfactory. decodefile (picpathstring, opts); // The obtained bitmap is the compressed bitmap.

Second: Using Matrix

// Obtain the width and height of the image. Int width = _ cameraimage. getwidth (); int hegith = _ cameraimage. getheight (); // create the matrix object matrix = new matrix (); int degree = 90; log. I (TAG, "degree --->" + degree); // rotate the image matrix. postrotate (degree); matrix. postscale (newscalewidth, newscaleheight); // scale the image // create a new image bitmap resizedbitmap = NULL; resizedbitmap = bitmap. createbitmap (_ cameraimage, 0, 0, width, hegith, matrix, true );

Third: Use canvas to redraw bitmap to obtain a zoom chart

 Rect r = bitmap. getcroprect (); // you need to write this method to obtain the area of the rectangle occupied by the original bitmap int width = R. width (); // Cr: Final = happy panda! Int Height = R. height (); bitmap newbitmap = NULL; newbitmap = bitmap. createbitmap (width, height, bitmap. config. argb_8888); {canvas = new canvas (newbitmap); rect dstrect = new rect (0, 0, new_width, new_height ); // specifies the size of the rectangle to which the source image is scaled. Paint = new paint (); paint. setantialias (true); canvas. drawbitmap (newbitmap, R, dstrect, paint); // scaled successfully}

Method 4: thumbnailutils

 
Bitmap newbitmap = NULL; newbitmap = thumbnailutils. extractthumbnail (imageaftercroped, maxweight, maxheight, options_recycle_input );

This is my summary of how to process images in a centralized and simple manner. Please refer to the source for more information. Thank you.

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.