Image scaling in Android development

Source: Internet
Author: User

When it comes to the processing of images in Android development, how do we usually scale the operation, and look at the following two ways:

Method 1: Scale at a fixed scale

In the development of some software, such as news client, a lot of time to display the thumbnail image, due to mobile phone screen restrictions, in general, we take pictures of thumbnails, thumbnail image method We can use the Bitmapfactory DecodeFile method, and then passed in Bitmapfactory.option the parameters of the type, in option, the property value insamplesize indicates that the thumbnail size is one of the dimensions of the original picture, that is, if the value is 2, the width and height of the thumbnail that is taken out are 1 of the original picture. 2, the image size is 1/4 of the original size. However, if we want to take a fixed size of the thumbnail is more difficult, for example, we want to take different sizes of the thumbnail height of the image is 200px, and to ensure that the picture is not distorted, then what should we do? You can't load the original picture into memory and scale it, you know, in mobile development, Memory is quite valuable, a picture of 100K, loading up the memory occupied is more than 100K.

After reading the document, there is an attribute in the options injustdecodebounds, as the document says:

If set to True, the decoder would return null (no bitmap), but the ... fields would still be set, allowing the caller to  Query the bitmap without has to allocate the memory for its pixels. This means that if the value is set to True then the actual bitmap object will not be returned, but no memory space is allocated but some information such as the image size can be obtained by decoding the boundary information. So we can get to Outheight (the original height of the picture) and Outwidth (the original width of the picture) by setting Injustdecodebounds to True, and then calculate a insamplesize (scale value), and you can take the picture, It is important to note that insamplesize may be equal to 0 and must be judged. That is, the options of the property injustdecodebounds set to True, first get the image of the basic size information data (information is not stored in the bitmap inside, but in the options), through Options.outheight and Options. Outwidth Gets the size information and the image size you want to calculate the scale insamplesize, and then immediately set the Injustdecodebounds to False, you can get the zoom scale of the image you want to have the picture.

The implementation code is as follows:

Bitmapfactory.options Options = new Bitmapfactory.options ();

Options.injustdecodebounds = true;

Remember to copy the images from the assets directory to the SD card.

Because the setting injustdecodebounds is true, bitmap is empty after executing the following code

Mbitmap = Bitmapfactory.decodefile ("/sdcard/image.jpg", options);

Calculate the scale, because it is a fixed scale, only one of the data is high or wide calculation can be

int scale = (int) (Options.outheight/(float) 200);

Because the result is of type int, if the value is 0.N after division, the final result will be 0

if (scale <= 0)

{

scale = 1;

}

System.out.println ("scale=" + scale);

Options.insamplesize = scale;

Options.injustdecodebounds = false;

Re-read the picture, note that the options.injustdecodebounds is now set back to False

Mbitmap = Bitmapfactory.decodefile ("/sdcard/image.jpg", options);

int width = mbitmap.getwidth ();

int height = mbitmap.getheight ();

System.out.println (width + "" + height);

ImageView image = (ImageView) Findviewbyid (r.id.image);

Image.setimagebitmap (MBITMAP);

This allows us to read larger images without a memory overflow problem.

If you want to save the compressed picture on the SDcard, you can use the following code:

File File = new file ("/sdcard/ruoshui.png");

Try

{

Remember to add SDcard Read and Write permissions

FileOutputStream out = new FileOutputStream (file);

if (mbitmap.compress (Bitmap.CompressFormat.PNG, +, Out))

{

Out.flush ();

Out.close ();

Toast.maketext (Mainactivity.this, "saved successfully!", Toast.length_long). Show ();

}

}catch (Exception e)

{

E.printstacktrace ();

Toast.maketext (Mainactivity.this, "Save failed!", Toast.length_long). Show ();

}

Method 2: Scale by the respective proportions of the length and width

The above method of scaling is saved by a length-to-width ratio, and we can of course scale by a fixed size:

Bitmapfactory.options Options = new Bitmapfactory.options ();

Options.injustdecodebounds = false;

Remember to copy the images from the assets directory to the SD card.

Because the setting injustdecodebounds is true, bitmap is empty after executing the following code

Mbitmap = Bitmapfactory.decodefile ("/sdcard/image.jpg", options);

int bmpwidth = Mbitmap.getwidth ();

int bmpheight = Mbitmap.getheight ();

Scale the size of a picture

float ScaleWidth = (float) swidth/bmpwidth; Scale swidth by fixed size how big it is.

float ScaleHeight = (float) sheight/bmpheight; //

Matrix matrix = new Matrix ();

Matrix.postscale (ScaleWidth, ScaleHeight);//Generate Scaled Bitmap object

Bitmap Resizebitmap = Bitmap.createbitmap (mbitmap, 0, 0, bmpwidth, Bmpheight, Matrix, false);

Mbitmap.recycle ();

ImageView image = (ImageView) Findviewbyid (r.id.image);

Image.setimagebitmap (RESIZEBITMAP);

Image scaling in Android development

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.