Bitmap optimization
Bitmap describing
One of the most important classes for Bitmap image processing. It can be used to obtain image file information and can be programmed.
Basic Bitmap functions
How much memory is used by Bitmap
Computational memory size: image pixel widthImage pixel heightStrictly speaking, memory size calculation: width * scale * height * scale * bytes taken up by a pixel Scale = the density of the phone on the screen/the density of the folder where the image is located
The Config parsing
- Bitmap.config. ALPHA_8: Each pixel is stored as a single translucent (alpha) channel. No color information is stored. With this configuration, each pixel requires 1 byte of memory
- Bitmap.config. ARGB_4444: This field is not recommended for API level 29. Because of the poor quality of this configuration, ARGB_8888 is recommended instead.
- Bitmap.config. ARGB_8888: Each pixel is stored on 4 bytes. Each channel (RGB and alpha for translucency) is stored with 8-bit accuracy (256 possible values). This configuration is very flexible and provides the best quality. It should be used whenever possible
- Bitmap.config. RGBA_F16: Each pixel is stored on 8 bytes. Each channel (translucent RGB and alpha) is stored as a semi-precision floating point value. This configuration is particularly suitable for wide gamut and HDR content.
- Bitmap.config.565: Each pixel is stored on 2 bytes, and only RGB channels are encoded: red stores 5 bits of accuracy (32 possible values), green stores 6 bits of accuracy (64 possible values), and blue stores 5 bits of accuracy. Accurate. Depending on the configuration of the source, this configuration may produce minor visual artifacts. For example, if there is no jitter, the result might be green. For better results, jitter processing should be used. This configuration can be useful when using opaque bitmaps that do not require high color fidelity
CompressFormat parsing
- Bitmap.com pressFormat. JPEG compression for the JPEG format. Mass 0 means compressed to minimum size. 100 represents compression for maximum visual quality.
- Bitmap.com pressFormat. PNG compression as PNG format. PNG is lossless, so quality is ignored
Manage bitmap memory
Android3.0 BitmapFactory is introduced. The Options. InBitmap fields. If this option is set, the encoding method using the Options object attempts to reuse the existing bitmap when loading content. This means that bitmap memory is reused for improved performance, while memory allocation and unallocation are removed. However, there are some limitations to how inBitmap can be used. Especially before Android4.4, the system only supported bitmaps of the same size.
Bitmap compression method
Bitmap.press (CompressFormat format, int Quality, OutputStream) method
Writes a compressed version of the bitmap to the specified output stream. If returns true, will be passed to the corresponding input to BitmapFactory. DecodeStream () to reconstruct the bitmap. Note: Not all formats directly support all bitmap configurations, so bitmaps returned from the BitmapFactory may have different bit depths and/or may be missing the alpha value for each pixel (for example, JPEG only supports opaque pixels).
Parameters | |
---|---|
format |
Bitmap.CompressFormat : Compressed image format |
quality |
int : Prompt compressor, 0-100. Depending on the CompressFormat, this value can be compressed differently. |
stream |
OutputStream : Output stream that writes compressed data. . |
Bitmap pixel compression mode
bitmapFactory.options.inSampleSize
If set to a value greater than 1, the decoder is asked to re-sample the original image, returning a smaller image to save memory. The sample size is the number of pixels in any dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 of the original width/height and 1/16 of the number of pixels. Any value less than or equal to 1 is the same as 1. Note: The decoder uses final values based on powers of 2, and any other values will be rounded to the nearest power of 2.
Screenshot Network code
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, Int reqHeight) {// Set inJustDecodeBounds to true to get only the original Bitmap width and no memory allocated; final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize; options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Actually load Bitmap; options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options); } public static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; InSampleSize while ((halfWidth/inSampleSize) >= reqHeight && (halfWidth/inSampleSize) >= reqWidth) { inSampleSize *= 2; } } return inSampleSize; }Copy the code