Android image sampling and scaling function example code

Why sample and zoom images in Android?

To load bitmap more efficiently. Assuming that the image is displayed through ImageView, in many cases, the ImageView is not as large as the original size of the image. At this time, it is not necessary to load the whole image and set it to ImageView, because ImageView cannot display the original image.

Therefore, we can use bitmapfactory.options to load the reduced image according to a certain sampling rate and display the reduced image in ImageView, so as to reduce the memory occupation, avoid oom to a certain extent and improve the performance during bitma loading.

Bitmapfactory has one parameter: insamplesize (sampling rate).

If insamplesize is 1, the image size after sampling is equal to the original image size.

If insamplesize is 2, the width and height of the sampled image are 1 / 2 of the original image, the pixel is 1 / 4 of the original image, and the occupied memory size is 1 / 4 of the original image.

For example, if the pixel of a picture is 1024 * 1024 and the storage format is argb8888, it occupies 1024 * 1024 * 4 = 4m of memory. After sampling with a sampling rate of 2, the memory occupation is 512 * 512 * 4 = 1m.

Summary: insamplesize must be an integer greater than 1 to be effective. Small and 1 are equivalent to 1, and act on width and height at the same time. Therefore, the scaled image size decreases in the form of the quadratic power of the sampling rate. According to the latest official documents, the value of insamplesize should always be an index of 2. If the insamplesize given to the system is not an index of 2, Then the system will round down and select an index closest to 2 to replace it. However, after verification, this conclusion is not true on all Android versions.

So how do we get the sampling rate?

1. Set the injustdecodebound parameter of bitmapfactory.option to true to load the picture. At this time, the picture is not loaded into memory, but only to parse the original width and height information of the picture.

2. Get the original width and height information of the picture from bitmapfactory.option, corresponding to the outwidth and outheight parameters.

3. Calculate the sampling rate insamplesize according to the rules of the sampling rate and the required size of the target original view.

4. Set the injustdecodebound parameter of bitmapfactory.option to false and reload the picture. At this time, the picture will really be loaded into memory.

A code template is provided below: package com.example.chatting.chatting.utils;

summary

The above is the sample code of Android image sampling and scaling function introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>