An overview,

Here are some basics about Bitmaps:

  • BitmapWhat is the
  • Concepts related to screen density
  • In the projectdrawablefolder
  • Bitmap.Options
  • The brief analysis ofBitmapThe memory of

Two, what isBitmap

Officially, a Bitmap is an abstraction of a Bitmap. Figuratively speaking, what we see on a mobile phone screen is a Mosaic of pixels, each pixel represented by a different number of binary bits, and a Bitmap is used to store these bits.

Three,BitmapOccupied Memory Analysis

3.1 Concepts related to screen density

As mentioned earlier, the ultimate purpose of bitmaps is to display images on the screen, so first we need to know something about screen density:

  • pixelpx: If we look closely at the screen of a mobile phone, we can see that it has dots, and each dot represents a pixel, which we usually callpx.
  • Screen resolution: refers toThe sum of pixels on a screen, for example, the screen resolution of a mobile phone is1920 * 1080In other words, its height is zero1920pxAnd for the wide1080px, the sum of pixels is1920 * 1080px.
  • Screen size: refers toThe object line length of the screenThe length here is notpxAs a unit, but in inches as a unit, it and we usually say meters, centimeters is the same concept.
  • ppi: English namepixel per inch, full name in ChineseNumber of pixels per inch on the screenIt decidesScreen qualityIt’s usually measured on the diagonal of the phone, which means it equalsThe number of pixels of the object line divided by the length of the diagonal in inches.
  • dp/dip: English namedensity-independent pixelThis is an Android-specific conceptpxBoth are used for length. But it is independent of the hardware screen if you need to convert topx, then1dpIt will eventually show up on the screen as(ppi / 160)apx.
  • dpi: English namedot per inch, full name in ChineseThe number of dots per inch of the pictureIt decidesPicture quality, such asdpifor320, then finally on the phone one320 * 320The picture will be used320 * 320 * (dpi / 160)In terms of pixels.

3.2 Dynamically obtaining the above information

In Android, DisplayMetrics provides the above information:

    public static void logDensityInfo(Activity activity) { DisplayMetrics displayMetrics = new DisplayMetrics(); / / save the information into the displayMetrics. Activity. GetWindowManager (.) getDefaultDisplay () getMetrics (displayMetrics); //1. Dpi.log.d (dpi.log.d)"logDensityInfo"."ydpi=" + displayMetrics.ydpi);
        Log.d("logDensityInfo"."xdpi="+ displayMetrics.xdpi); //2. Number of x and y pixels. Log.d("logDensityInfo"."heightPixels=" + displayMetrics.heightPixels);
        Log.d("logDensityInfo"."widthPixels=" + displayMetrics.widthPixels);
        //3.dpi
        Log.d("logDensityInfo"."densityDpi=" + displayMetrics.densityDpi);
        //4.dpi/160.
        Log.d("logDensityInfo"."density="+ displayMetrics.density); // Density is usually the same as density. Log.d("logDensityInfo"."scaledDensity=" + displayMetrics.scaledDensity);
    }
Copy the code

On my phone, the final result was:

dpi

    public void setToDefaults() {
        density =  DENSITY_DEVICE / (float) DENSITY_DEFAULT;
        densityDpi =  DENSITY_DEVICE;
        scaledDensity = density;
        xdpi = DENSITY_DEVICE;
        ydpi = DENSITY_DEVICE;
    }
Copy the code

DENSITY_DEFAULT is 160, and DENSITY_DEVICE is obtained as follows:

private static int getDeviceDensity() {
    return SystemProperties.getInt("qemu.sf.lcd_density", SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));
}
Copy the code

3.3 drawablePictures in folders

Drawable -? Drawable -? Folder. In normal development, we will find that if images of the same size are placed under different Drawable folders, the final size displayed on the screen will be different. Density = density = density = density = density = density = density = density

  • Exists in a match with the cell phonedpiFolders: Do not zoom.
  • Exists in thedrawable-nodpiThe rest of thedpiFolder: then the final length becomes(Original length/folder density) * Matches folder density
  • There aredrawable-nodpi: Does not scale.

The binary number of each ARBG bit is multiplied by the binary number, which is the memory size of the image. The density is as follows:

  • drawable-nodpi: no zoom
  • drawable-ldpi:0.75
  • drawable:1
  • drawable-mdpi:1
  • drawable-hdpi:1.5
  • drawable-xhdpi:2
  • drawable-xxhdpi:3
  • drawable-xxhdpi:4

If a resource exists under more than one of the above folders, its matching priority is as follows:

  • And the mobile phonedpiThe matchingdpifolder
  • Than the matchdpihighdpifolder
  • drawable-nodpi
  • Than the matchdpiLow, but greater than or equal to1thedpifolder
  • drawable
  • drawable-ldpi

3.4 Bitmap.Config

This is an enumerated type that describes how each pixel is stored, which affects the quality of the image and determines whether transparent/translucent colors can be represented.

  • ALPHA_8: Each pixel represents onlyalphaIt does not store any color information accounted foreight.
  • RGB_565: per pixel5 bit R/6 bit G/5 bit GI’m going to say, occupy16.
  • ARGB_8888: Each pixel is used separatelyeightstorageARGB, accounting for32 -.
  • ARGB_4444And:8888Similar, but used for each channelfourTherefore, its image quality is relatively low and is no longer recommended.

3.5 Case Analysis

Having introduced the basic knowledge needed to master, it can be concluded that the size of Bitmap memory is actually determined by two factors:

  • Width height read into memory
  • ARGBThe placeholder wide

The factors influencing the first point include the width and height of the original picture, the built-in DPI of the phone, and the position of the picture. The second factor is the bitmap. Config configuration of the Bitmap.

Here are a few examples to verify the three cases mentioned above:

3.5.1 Store them in a matching folder

  • The mobile phone resolution used is720 * 1280, the folder it matches isdrawable-xhdpi, we first set an original size as48 * 48The picture of thedrawable-xhdpiFolder, and configureOptionforBitmap.Config.RGB_565:
    private void logWrapperImageView() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.drawable_test, options);
        Log.d("logWrapperImageView"."width=" + bitmap.getWidth() + ",height=" + bitmap.getHeight() + ",size=" + bitmap.getByteCount());
    }
Copy the code

The end result in this case is that, although we indicated the need for RGB_565, the system still uses only one bit per pixel:

width=48,height=48,size=2304 //RGB_565
width=48,height=48,size=9216 //ARGB_8888
width=48,height=48,size=2304 //ALPHA_8
Copy the code
  • The mobile phone resolution used is1080 * 1920, the folder it matches isdrawable-xxhdpi, we first set an original size as48 * 48The picture of thedrawable-xxhdpiFolder, and configureOptionforBitmap.Config.RGB_565, run the same program as above, and get the result:
width=48,height=48,size=2304 //RGB_565
Copy the code

Then change its Options:

width=48,height=48,size=9216 //ARGB_8888
width=48,height=48,size=2304 //ALPHA_8
Copy the code

3.5.2 Put in mismatch, and is notdrawable-nodpiIn the folder

  • The mobile phone resolution used is720 * 1280, put the picture ondrawable-xxhdpiFolder:
width=32,height=32,size=4096 //RGB_565
Copy the code

Change its Options:

width=32,height=32,size=4096 //ALPHA_8
width=32,height=32,size=4096 //ARGB_8888
Copy the code
  • The mobile phone resolution used is1080 * 1960, put the picture ondrawable-xhdpiFolder:
width=72,height=72,size=20736
Copy the code

Change its Options

width=72,height=72,size=20736 //ALPHA_8
width=72,height=72,size=20736 //ARGB_8888
Copy the code

3.5.3 indrawable-nodpifolder

  • The mobile phone resolution used is720 * 1280:
width=48,height=48,size=2304 //RGB_565
width=48,height=48,size=9216 //ARGB_8888
width=48,height=48,size=2304 //ALPHA_8
Copy the code
  • The mobile phone resolution used is1080 * 1960:
width=48,height=48,size=2304 //RGB_565
width=48,height=48,size=9216 //ARGB_8888
width=48,height=48,size=2304 //ALPHA_8
Copy the code

3.5.4 summary

From the above examples, several points can be drawn:

  • Put the resource in its correspondingdpiFolder, and putdrawable-nodpiThe folders are the same.
  • We use theRGB_565, the actual usage is one byte.
  • When placing a resource file in a different folder, whichever is selectedOptions, are using occupancy4 bytesThe way.
  • When the resource is placed in the correspondingdpiUnder folder sumdrawable-nodpiUnder the folder, length and width are not zoomed.
  • When a resource is placed in a folder other than the above two categories, the scaling factor isMatch folder density/ folder densityTo the above3.5.2the720 * 1280For example, the length and width of the original picture are48, which matches the folderdensityfor2, of the folderdensityfor3, so the scaling factor isTwo-thirds of the, therefore, the final length and width are32.
  • BitmapThe memory footprint is equal to its width times the number of bits per pixel.