Detailed explanation of efficient loading and use of bitmap in Android Development

Due to the memory limit imposed by Android on a single application, such as 16MB, it is easy to have memory overflow when loading bitmap. This paper mainly includes two aspects to analyze bitmap memory and efficient loading of bitmap

1、 Occupied memory

Get the memory of bitmap. The method bitmap. Getbytecount () provided by Android. If there is a 200 * 200 pixel picture in the mipmap xhdpi directory, run and load it to see its output size.

Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.btn_go); The output result of bitmap.getbytecount() is 360000. Now transfer the picture to mipmap xhdpi. The output result of bitmap.getbytecount is 160000. Why is the mipmap xhdpi directory so large?

Density affects that bitmap memory is placed in different mipmap directories, corresponding to devices with different densities. Density is the inherent parameter of the device. Along with density, there is also density DPI, which is also related to the device. It indicates how many points (non pixels) correspond to each inch of the screen

The above is a classic picture provided by the official. You can see that different directories represent different densities. For example, the density represented by xhdpi is 2. Here, the reference of density to densitydip is 160, that is, the densitydpi corresponding to MDPI is 160, the densitydpi corresponding to xhdpi is 320, and similarly, the densitydpi corresponding to xxhdpi is 480

Both density and density DPI have standard APIs in Android, as shown below.

This is the output of my mobile phone

From the source code of getbytecount(), bitmap. Getbytecount() is composed of: (bitmapwidth * scale) * (bitmapheight * scale)* (byte size per pixel), in which: int scale = phonedensity / indensity phonedensity is the densitydpi of our mobile phone. The above results show that our mobile phone is 480, and indensity is the path where your pictures are stored. For example, xhdpi is 320, xxhdpi is 480, bitmapfactory default color is argb_8888 pictures, and each pixel will occupy 4 bytes. Under xhdpi: 200 * (480 / 320) *200 (480 / 320) 4 = 360000 under xxhdpi: 200 * (480 / 480) * 200 (480 / 480) 4 = 160000

The above is the memory analysis of bitmap.

2、 Efficient loading

1. Modify bitmap.config 2. Modify insamplesize

Introduction to bitmap.config

As mentioned above, the default color level of bitmapfactory is ARGB_ eight thousand eight hundred and eighty-eight

There are four parameters in bitmap.config as follows: (these parameters determine the configuration of bitmap and affect the pixels, color and transparency of bitmap.)

Bitmap.Config ALPHA_ eight

This parameter takes up 1 byte of space per pixel. It represents that each pixel is stored as a single transparency channel, which is very useful for picture use cases with masks. It does not store color information.

Bitmap.Config RGB_ five hundred and sixty-five

This parameter takes up 2 bytes of space per pixel. It represents the code of only RGB channel, in which red occupies 5-bit address, green occupies 6-bit address and blue occupies 5-bit address. Channels without transparency. When using opaque bitmaps, high color fidelity is not required. This configuration is a good choice.

Bitmap.Config ARGB_ four thousand four hundred and forty-four

This parameter takes up 2 bytes of space per pixel. It has four channels. As the name suggests, they are transparency, red, green and blue. Each channel occupies four bit addresses, so there are 2 bytes in total. When the application needs to save memory (low requirements for color quality) and store transparency information, this configuration can be used as an option, but ARGB is recommended by the official_ 8888 setting, because the color quality of this is poor.

Bitmap.Config ARGB_ eight thousand eight hundred and eighty-eight

This parameter takes up 4 bytes of space per pixel. This is also a total of four channels, but the difference is that each channel station has an 8-bit address, so the color quality is much higher (16 times) than the previous setting. It can meet the best bitmap quality. This is highly recommended when there is enough memory.

Introduction to insamplesize

Bitmapfactory.options is used to scale the picture. The insamplesize parameter is mainly used. When insamplesize = 1, the sampled picture is the original size of the picture. When insamplesize = 2, the width and height of the sampled picture are 1 / 2 of the original size, and the number of pixels is 1 / 4 of the original size. Assuming that the original memory of the picture is 4MB, if its insamplesize is set to 2, Its memory will become 1MB, and the specific implementation code is as follows

With the above two methods, the actual use is simple. For example, the image size expected by ImageView is 100x100. At this time, we can call it like this. It is still the image previously stored in xxhdpi. In the above code, bitmap.config has been set to ARGB_ 4444. Now change the original size from 200x200 to 100x100 to see how much memory is

The output result of bitmap.getbytecount is 20000, which is 8 times less than the previous 160000, ARGB_ 4444 ARGB_ 8888 is reduced by 2 times. Set the size to 100100, and pass it into the setinsamplesize() method to get insamplesize = 2. The number of pixels is 1 / 4 of the original image, and the total memory size becomes 1 / 8 of the previous one. In this way, the image will be loaded efficiently and away from oom.

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
分享
二维码
< <上一篇
下一篇>>