This is the fifth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

Bitmap loads images

Android uses BitmapFactory to load image resources.

BitmapFactoryThere are four ways to load Bitmap resources:decodeFile(document), decodeResource(Resource file),decodeStream(Input stream),decodeByteArray(Byte array).The loading logic of images in Android is implemented in Native base layer. The four loading forms introduced above are also implemented in the following methods.

In general, Bitmap loading will not load the original size of the image, and small resource images will not cause memory problems. However, large high-resolution images can easily cause the application to flash back in OOM. Therefore, BitmapFactory also has Options to control the size of Bitmap loading to achieve efficient image loading.

BitmapFactory.Options

You can configure Options to reduce image memory usage and reduce flash back caused by image display. InSampleSize and inJustDecodeBounds are two parameters that Options often use.

  • inSampleSize

If inSampleSize=1, the image size is the original size. If inSampleSize is greater than 1, the original image size is reduced. For example, if inSampleSize= 2,100×100, the image size is reduced to 50×50. And inSampleSize is >=1 and has no magnification. For example, if you take an image stored in 1024×1024 ARGB8888 format, the original image size is 4MB. Set inSampleSize to 2 to reduce the sampling size by 512×512 and the compressed size by 2MB, which is still quite impressive. But you also need to control the inSampleSize assignment, too much compression can cause image distortion.

  • inJustDecodeBounds

InJustDecodeBounds is also an important configuration parameter. When inJustDecodeBounds is true, only basic information about the image (width, height, image format, etc.) is retrieved, and the Bitmap object is empty. This is because the decoding process value obtains the image information and does not load the image resources. This lightweight operation will not result in an OOM. InJustDecodeBounds is often used to perform pre-compression operations on an image to obtain information and determine compression strategies.

The following code is commonly used according to the desired output destWidth, destHeight size of the image and the original image of the original size to calculate the inSampleSize to get the final image.

Options opt = new Options();
opt.inJustDecodeBounds = true; /// Set to true when decoding image information
BitmapFactory.decodeResource(getResources(),"xxxxx",opt);
double scaleW = Math.max(destWidth, opt.outWidth) / (Math.min(destWidth, opt.outWidth) * 1.0);
double scaleH = Math.max(destHeight, opt.outHeight) / (Math.min(destHeight, opt.outHeight) * 1.0);
opt.inSampleSize = (int) Math.max(scaleW, scaleH); 
opt.inJustDecodeBounds = false;/// Set to false when decoding images
bitmap = BitmapFactory.decodeResource(getResources(),"xxxxx", opt); // Get the image after changing the sampling rate
return bitmap;
Copy the code

conclusion

In addition to configuring bitmapFactory. Options to load images that meet memory requirements, there are other ways to compress images to load, such as quality compression, zoom compression, image format compression, etc. More compression strategies for efficient image loading.