One, foreword

It has been a long time since I updated my blog for about half a year. The main reason is that the blogger is busy looking for a job during this period. It is really getting harder and harder to find a job for Android position. Now I have been working as an intern in the company for more than a month. I am mainly responsible for memory optimization of a product of the company. I just summarized the commonly used optimization methods of Android Bitmap.

Images in Android exist in the form of Bitmap, and the drawing process is also a Bitmap, which directly affects the memory of app running. On Android, the memory occupied by Bitmap is calculated by the formula: picture length x picture width x number of bytes of pixels

Two, the picture commonly used compression format

Enum Values
ALPHA_8 Each pixel is stored as a translucent (alpha) channel
ARGB_4444 This field is deprecated at API level 13. Because of the poor quality of this configuration, ARGB_8888 is recommended
ARGB_8888 Each pixel is stored in 4 bytes.
RGB_565 Each pixel is stored in 2 bytes, and only RGB channels are encoded: red is stored with 5 bit accuracy (32 possible values), green is stored with 6 bit accuracy (64 possible values), and blue is stored with 5 bit accuracy.

We can probably understand what the letters mean, but let’s count the number of bytes per pixel:

  • ALPHA_8: represents an 8-bit Alpha bitmap, that is, transparency takes up 8 bits, one pixel takes up 1 byte, it has no color, only transparency.
  • ARGB_4444: represents A 16-bit ARGB bitmap, i.e. A=4,R=4,G=4,B=4, A pixel of 4+4+4=16 bits, 2 bytes.
  • ARGB_8888: represents the 32-bit ARGB bitmap, i.e. A=8,R=8,G=8,B=8, A pixel of 8+8+8+8=32 bits, 4 bytes.
  • RGB_565: represents a 16-bit RGB bitmap, that is, R=5,G=6,B=5, it has no transparency, a pixel of 5+6+5=16 bits, 2 bytes

When we do compression, we can first change the Bitmap image format to achieve compression effect. In fact, the most important compression is either to change its width and height, or to reduce the memory occupied by a single pixel.

Three, commonly used compression methods:

1. Quality compression

    private void compressQuality() {
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.test);
        mSrcSize = bm.getByteCount() + "byte";
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        byte[] bytes = bos.toByteArray();
        mSrcBitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }
Copy the code

Quality compression does not reduce the pixel size of the image. It changes the bit depth and transparency of the image on the premise of maintaining the pixel size to achieve the purpose of image compression. The length, width and pixel of the image will not change, so the memory size of the bitmap will not change.

We can see that there is a parameter called quality, which can adjust the ratio of your compression, but also note that the quality compression heap PNG format does not work because PNG is lossless compression.

2. Sampling rate compression

    private void compressSampling() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        mSrcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test, options);
    }
Copy the code

The principle of sampling rate compression is actually scaling the size of the bitamp. By adjusting the inSampleSize parameter, for example, to 2, the width and height will be 1/2 of the original size and the memory will be 1/4 of the original size.

3. Compress by scaling

    private void compressMatrix() { Matrix matrix = new Matrix(); Matrix. SetScale (0.5 0.5 f, f); Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.test); mSrcBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix,true);
        bm = null;
    }
Copy the code

Scaling compression uses a matrix to crop the picture, and also by scaling the size of the picture to achieve the effect of compression, the same principle as the sampling rate.

4. RGB_565 compression

    private void compressRGB565() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        mSrcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test, options);
    }
Copy the code

ARGB_4444 is generally not recommended because of the quality of the image. If there is no requirement for transparency, it is recommended to use RGB_565, which saves half of the memory cost compared to ARGB_8888.

5.createScaledBitmap

    private void compressScaleBitmap() {
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.test);
        mSrcBitmap = Bitmap.createScaledBitmap(bm, 600, 900, true);
        bm = null;
    }
Copy the code

Compress the image size to the desired size to reduce memory footprint.

Four, effect drawing

Five, the summary

The above five compression methods are commonly used, and the compression here is only for the memory size of the bitmap loaded during the run. When we do App memory optimization, we can generally start from the following two aspects: one is memory leakage, and the other is Bitmap compression. In the case of low pixel requirements, Bitmap compression can be carried out, and some bitmaps that are used only once should be well recycle processing.

Here is the source address. ,