Choose the right image size

  • ALPHA_8 occupies 1byte of memory per pixel
  • ARGB_4444 occupies 2 bytes of memory per pixel
  • ARGB_8888 takes up 4 bytes of memory per pixel (default)
  • RGB_565 occupies 2 bytes of memory per pixel

Reduced sampling rate

The principle of

Sampling rate compression is to change the pixels of the picture by reading the edges of the picture first, then setting the edges of the picture, and then reading the pixels of the picture according to the Settings. At the time of reading, not all pixels are read, but selected.

For example, if a picture of 1024 x 1024 pixels is stored in ARGB8888 format, the memory size is 1024 x 1024 x 4=4M. If inSampleSize=2, then the image memory size after sampling: 512×512×4=1M.

Note: the value of inSampleSize should always be an index of 2, such as 1,2,4,8, etc. If the value of inSampleSize is not an exponent of 2, the system will round down and select the nearest exponent of 2 instead. For example, 3, the system will choose 2 instead. Experience at the time proved that this was not true for all Android versions.

Sampling rate compression process

  1. Set bitmapFactory. Options to True and load the image.
  2. Extract the original width and height information for the image from bitmapFactory. Options, which correspond to the outWidth and outHeight parameters.
  3. Calculate the sample rate inSampleSize based on the rule of the sample rate and the desired size of the target View.
  4. Set bitmapFactory. Options to false to inJustDecodeBounds and reload the image.

Reuse memory

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void initBitmap(a) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    // Image reuse, this property must be set;
    options.inMutable = true;
    // Manually set the zoom ratio, so that it is an integer, convenient calculation, observation data;
    options.inDensity = 320;
    options.inTargetDensity = 320;
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg_autumn_tree_min, options);
    // Object memory address;
    Log.i("ycBitmap"."bitmap = " + bitmap);
    Log.i("ycBitmap"."ByteCount = " + bitmap.getByteCount() + :::bitmap: AllocationByteCount =" + bitmap.getAllocationByteCount());
    // Use the inBitmap attribute, which must be set;
    options.inBitmap = bitmap; options.inDensity = 320;
    // Set the zoom width to half of the original width and height;
    options.inTargetDensity = 160;
    options.inMutable = true;
    Bitmap bitmapReuse = BitmapFactory.decodeResource(getResources(), R.drawable.bg_kites_min, options);
    // Reuse the memory address of the object;
    Log.i("ycBitmap"."bitmapReuse = " + bitmapReuse);
    Log.i("ycBitmap"."bitmap:ByteCount = " + bitmap.getByteCount() + :::bitmap: AllocationByteCount =" + bitmap.getAllocationByteCount());
    Log.i("ycBitmap".BitmapReuse: ByteCount =" + bitmapReuse.getByteCount() + ":::bitmapReuse:AllocationByteCount = " + bitmapReuse.getAllocationByteCount());

    //11-26 18:24:07.971 15470-15470/com.yc.cn. Ycbanner I/ycBitmap: bitmap = android.graphics.Bitmap@9739bff
    / / 11-26 18:24:07. 972, 15470-15470 / com. Yc. Cn. Ycbanner I/ycBitmap: Bitmap: ByteCount = 4346880:: Bitmap: AllocationByteCount = 4346880
    Ycbanner I/ycBitmap: bitmapReuse = android.graphics.Bitmap@9739bff
    / / 11-26 18:24:07. 994, 15470-15470 / com. Yc. Cn. Ycbanner I/ycBitmap: Bitmap: ByteCount = 1228800:::bitmap: AllocationByteCount = 4346880
    / / 11-26 18:24:07. 994, 15470-15470 / com. Yc. Cn. Ycbanner I/ycBitmap: BitmapReuse: ByteCount = 1228800:::bitmapReuse: AllocationByteCount = 4346880
}
Copy the code

Use the recycle() method to recycle memory in a timely manner

Reference:

Android performance Optimization (5) in detail Bitmap

Thoroughly understand Bitmap efficient loading strategies

Compression mode of Bitmap

Common compression methods for Android Bitmap