Android - Bitmap.createScaledBitmap () parameter Config to ARGB_8888

For the first time I had a question that, despite a thorough search, it seems that they have not yet asked.

I have a problem with Bitmap.createScaledBitmap() , as a result, the resulting scaled raster map is always ARGB_8888 , regardless of the input configuration. Naturally, this is a problem when working with limited memory.

  InputStream is; try { is = mAssets.open("test.png"); BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inPreferredConfig = Config.ARGB_4444; Bitmap originalBitmap = BitmapFactory.decodeStream(is, null, opts); System.out.println("Original Config: " + originalBitmap.getConfig()); mScale = (float) mHeight / originalBitmap.getHeight(); mBitmapScaled = Bitmap.createScaledBitmap(originalBitmap, (int)(mScale*(float)originalBitmap.getWidth()), (int)(mScale*(float)originalBitmap.getHeight()), true); System.out.println("Scaled: " + mBitmapScaled.getConfig()); originalBitmap.recycle(); is.close(); } catch (IOException e) { // Do something. } 

The above code returns the outputs:

Source Bitmart: ARGB_4444

Scaling: ARGB_8888

Since the Bitmap.createScaledBitmap() method does not accept the configuration, there is no way to prevent this. Any ideas?

+4
source share
1 answer

createScaledBitmap(...) creates a new, scaled bitmap and, therefore, passes your provided arguments to the createBitmap(...) method.

The following is a snippet from the createBitmap(...) source code:

  if (config != null) { switch (config) { case RGB_565: newConfig = Config.RGB_565; break; case ALPHA_8: newConfig = Config.ALPHA_8; break; //noinspection deprecation case ARGB_4444: case ARGB_8888: default: newConfig = Config.ARGB_8888; break; } } 

Each bitmap with the configuration ARGB_4444 converted to a bitmap ARGB_8888 , as you can see. Therefore, to answer your question: No, there is no way to prevent this (if you do not want to copy parts of the source code of Bitmap.java and create your own scaling method).

Why are bitmaps with the configuration ARGB_4444 converted to ARGB_8888 ?

The documentation states the following:

ARGB_4444 :

This field is outdated. Due to the poor quality of this configuration, it is recommended to use ARGB_8888 .

+4
source

Source: https://habr.com/ru/post/1487335/


All Articles