Proportion of custom pictures in Android

Source: Internet
Author: User

Although, there used to be the kind of photo album inside, and then, by cutting the picture to get their own desired picture, then for if you want a batch of pictures of the processing effect, then, relative to the cut, we automatically in the code to achieve the proportion of the image we want to cut. Then come to get the proportions of the picture you want.

In fact, this is the first to convert the picture to bitmap, and then put it into the byte, and then according to their own proportions to get the compression of the picture, finally get.

Here is the key code: Then the implementation of this method is okay:

    //To define a method to make the image     PublicBitmap Getbitmap (byte[] Data,intWidthintheight) {Bitmap Bitmap=NULL; Options opts=NewOptions (); Opts.injustdecodebounds=true; Opts.insamplesize= 2; Opts.injustdecodebounds=false; Bitmap= Bitmapfactory.decodebytearray (data, 0, Data.length, opts); returnbitmap; }

You'll see that you'll need a parameter here, which is a byte, and of course, this byte is the okay of the image you need to convert to this byte:

//First to change the bitmapResources res =getresources (); Bitmap BMP=Bitmapfactory.decoderesource (res, r.drawable.demo); //To get the byteBytearrayoutputstream BAOs =NewBytearrayoutputstream (); Bmp.compress (Bitmap.CompressFormat.PNG,100, BAOs); byte[] data =Baos.tobytearray (); Bitmap bit= Getbitmap (data, 300, 300); //To exchangeBitmapdrawable draw =Newbitmapdrawable (bit); Img_two.setbackground (draw);

The above is to change the image of a resource into bitmap form, and then in Byte, then 叜 call the previous conversion method, and then return a bitmap, and then use the corresponding control to get the okay.

Hope this is useful for everyone!

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.