Bitmap explanation of Android image cache (I)

Foreword: I'm going to study the picture caching framework recently. Based on this idea, I think I'd better understand the basic knowledge of picture caching first. Today, I'll focus on bitmap and bitmapfactory.

Bitmap: bitmap is one of the most important classes of image processing in Android system. It can be used to obtain the image file information, cut, rotate and zoom the image, and save the image file in a specified format. Important functions • public void recycle() / / reclaim the memory space occupied by the bitmap and mark the bitmap as dead • public final Boolean isrecycled() / / judge whether the bitmap memory has been released • public final int getwidth() / / get the width of the bitmap • public final int getheight() / / get the height of the bitmap • public final Boolean ismutable()) //Whether the image can be modified • public int getscaledwidth (canvas) / / obtain the width of the image after the specified density conversion • public int getscaledheight (canvas) / / obtain the height of the image after the specified density conversion • public Boolean compress (compressformat, int quality, OutputStream stream) / / according to the specified image format and image quality, Converts a picture to an output stream. format:Bitmap. CompressFormat. Png or bitmap CompressFormat. JPEG quality: image quality, 0-100.0 indicates the lowest image quality compression, and 100 indicates the highest image quality compression. For pictures in lossless formats such as PNG, this setting will be ignored. • Public static bitmap createbitmap (bitmap SRC) / / use SRC as the original image to generate an image that cannot be changed • public static bitmap createscaledbitmap (bitmap SRC, int dstwidth, int dstheight, Boolean filter) / / use SRC as the original image to create a new image, and specify the height and width of the new image and whether it can be changed. • Public static bitmap createbitmap (int width, int height, config) - creates a bitmap with a specified format and size • public static bitmap createbitmap (bitmap source, int x, int y, int width, int height) creates a new image with source as the original image, and specifies the starting coordinates and the height and width of the new image.

Bitmapfactory factory class: option parameter class: • public Boolean injustdecodeboundaries / / if set to true, the image will not be obtained and memory will not be allocated, but the height and width information of the image will be returned. • Public int insamplesize / / multiple of image scaling • public int outwidth / / get the width value of the image • public int outheight / / get the height value of the image • public int indensity / / pixel compression ratio for bitmap • public int intargetdensity / / pixel compression ratio for target bitmap (bitmap to be generated) • public byte [] intempstorage / / create a temporary file, Set the image storage • public Boolean inscaled / / to true for image compression, from indensity to intargetdensity • public Boolean indither / / if true, the decoder attempts jitter decoding • public bitmap Config inpredconfig / / set the decoder • public string outmimetype / / set the decoded image • public Boolean inpluggable / / whether the memory space for storing pixels can be recycled when the system memory is insufficient • public Boolean inputshareable / / inpluggable takes effect only when it is true, Whether an InputStream can be shared • public Boolean inpredqualityoverspeed / / if it is true, the quality of the bitmap will be guaranteed first, followed by the decoding speed • public Boolean inmutable / / whether the configuration of the bitmap can be changed, such as adding a line segment every few pixels on the bitmap • public int inscreen density / / the pixel density of the current screen

Factory method: • public static bitmap decodefile (string pathname, options opts) / / read pictures from files • public static bitmap decodefile (string pathname) • public static bitmap decodestream (InputStream is) / / read pictures from input streams • public static bitmap decodestream (InputStream is, rect outpadding, options opts) • public static bitmap decoderesource (resources res, int ID) / / read the picture from the resource file • public static bitmap decoderesource (resources res, int ID, options, opts) • public static bitmap decodebyte array (byte [] data, int offset, int length) / / read the picture from the array • public static bitmap decodebyte array (byte [] data, int length, options, opts) • public static bitmap decodefiledescriptor (filedescriptor FD) / / reading a file from a file is different from the decodefile in that it is more efficient to directly call the JNI function • public static bitmap decodefiledescriptor (filedescriptor FD, options opts)

Bitmap. Config inpredconfig: enumeration variables (the higher the bit number of the bitmap, the more color information it can store, the more realistic the image and the larger the memory it takes) • public static final bitmap Config ALPHA_ 8 / / represents an 8-bit alpha bitmap. Each pixel occupies 1 byte of memory • public static final bitmap Config ARGB_ 4444 / / represents a 16 bit ARGB bitmap. Each pixel occupies 2 bytes of memory • public static final bitmap Config ARGB_ 8888 / / represents a 32-bit ARGB bitmap. Each pixel occupies 4 bytes of memory • public static final bitmap Config RGB_ 565 / / represents an 8-bit RGB bitmap. Each pixel occupies 2 bytes of memory

The memory occupied by a picture (bitmap) in Android is mainly related to the following factors: picture length, picture width, and the number of bytes occupied per pixel.

Memory occupied by a picture (bitmap) = picture length * picture width * bytes occupied per pixel

Image reading example: 1.) Read from file mode 1

2.) the efficiency of reading from file mode 2 is higher than that of mode 1

The test also generates 10 pictures. The two methods are time-consuming, and the CPU usage and memory occupation are almost the same. The second method is a little more efficient, so it is recommended to give priority to the second method

3.) read the file from the input stream

4.) read the file from the resource file

This method consumes considerable memory. It is recommended to use decodestream instead of decoderesource, which can be in the following form

Comparison of memory occupied by decodestream and decoderesource:

BitmapFactory. The pictures loaded by decoderesource may be scaled. At present, the scaling is done in the Java layer, which is inefficient and consumes the memory of the Java layer. Therefore, if you use this interface heavily to load pictures, it is easy to cause oom errors. BitmapFactory. Decodestream does not scale the loaded pictures. In contrast, it occupies less memory and is more efficient. These two interfaces are useful. If performance requirements are high, decodestream should be used; If you don't have high performance requirements and need the self-adaptive image scaling function of Android, you can use decoderesource.

5.) reading pictures from binary data

6.) read the picture from the assets file

Picture save file:

Picture compression:

Picture zoom:

Get picture rotation angle:

Picture rotation angle:

Picture to binary:

Bitmap to drawable

Drawable to bitmap

Afinal and xutils have been used before the discussion on memory occupation of drawable and bitmap. Those familiar with these two frameworks know that they come from the same person. Xutils is an upgraded version of afina. The image memory cache in afinal uses bitmap. Why did xutils change the object of memory cache to drawable later? Let's find out and write a test program:

Test data 1000 same picture

The test shows that drawable has great memory occupation advantages over bitmap. This is why the main picture caching framework uses drawable as the caching object in the memory caching layer. Summary: I'll learn about image processing here for the time being and supplement it later.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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