Qi:

Before we look at secondary sampling, we need to understand some image specifications. In our daily app development, most of the time, we need to display the picture information, the picture finally corresponds to the Android Bitmap object. There are roughly four kinds of Bitmap specifications:

  • ALPHA_8:
  • ARGB_4444:
  • ARGB_8888:
  • RGB_565:

However, Bitmap is a troublesome problem for the APP side, mainly reflected in the fact that Bitmap is a very memory consuming object, improper processing will lead to APP running lag or even OOM. The problem the blogger encountered last time is the use of XRecycleview to pull up and pull down. Image loading caused by too much sliding stuck, so at this time, it is necessary to use picture optimization!

Why use secondary sampling?

Maybe the above is not detailed enough, now I will talk about the reasons for using it in detail:

When we are showing a 100M image, we usually use a picture control to display it, so your 100M
Does the big picture show up? OOM exception may be reported, which involves the problem of memory overflow when loading pictures, and this time will use secondary sampling.

In summary: Secondary sampling avoids OOM exceptions when loading images.

PS: OOM (OutOfMemoryError) Memory overflow is abnormal.

What is the purpose and operation of secondary sampling?

First sampling:

The first sampling is basically taking the image and setting it to the size we want.

/ / this property is set to true will only load the border around the image, the image will not specific pixels options. InJustDecodeBounds = true;Copy the code

When we load the image in, we first get the width and height of the image, this load will not cost a lot of memory, combined with the width and height of the control we are defining, calculate the scale we need to scale. The fact that we display a very high resolution image and a low resolution image on our phones, as long as it’s not too small, doesn’t matter much.

Second sampling:

On the basis of the previous sample, a second sample is taken. At the scale that we’ve successfully calculated,

To set up the first

options.inJustDecodeBounds = false;
Copy the code

Second sampling need to load the picture out, first pass parameters to the result of the first sampling BitmapFactory, scaling, so as not to load the image came in, but only to load a thumbnail to come in, you will not only improve the loading speed, also save on memory, on the user experience, also won’t have too much difference.

Code display:

Public class BitmapUtils {/** ** @param filePath The image path to load ** @param destWidth The width of the control to display the image ** @param destHeight * */ public static Bitmap getBitmap(String filePath, int destWidth, Bitmapfactory.options Options = new bitmapFactory.options (); / / this property is set to true will only load the border around the image, the image will not specific pixels options. InJustDecodeBounds = true; Bitmapfactory. decodeFile(filePath, options); // Get the width and height of the original image int outWidth = options.outWidth; int outHeight = options.outHeight; Int sampleSize = 1; While (outHeight/sampleSize > destHeight | | outWidth/sampleSize > destWidth) {/ / if the width of high scaling of either party not up to par, //sampleSize should be set to the NTH power of 2. If the sampleSize is not set to the NTH power of 2, the nearest value is sampleSize *= 2; } // Now that the first sampleSize is complete, we have successfully calculated the size of the sampleSize // When the second sampleSize is started // When the second sampleSize is started, I need to load the image to display. Therefore inJustDecodeBounds attribute should be set to false options. InJustDecodeBounds = false; // Set the zoom scale options.inSampleSize = sampleSize; options.inPreferredConfig = Bitmap.Config.ARGB_8888; // Load the image and return bitmapFactory.decodefile (filePath, options); }}Copy the code

If you like it, you can support the blogger!

Reference blog: Jane book secondary sampling