In my app I load an image as 32 bit (ARGB_8888) this way:
Bitmap.Config mBitmapConfig;
mBitmapConfig = Bitmap.Config.ARGB_8888;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = mBitmapConfig;
mBitmap = BitmapFactory.decodeFile(SourceFileName, options);
Then scale:
mBitmap = Bitmap.createScaledBitmap(mBitmap, iW, iH, true);
If I use for scaling the same Width and Height of the original bitmap, it is 1/2 of the size in megabytes (I’m watching the heap size).
Changing the value “ARGB_8888” to “RGB_565” (24 bit) gives the same size in megabytes after scaling.
Can someone explain this phenomenon and may be give me an advice, how to scale bitmaps in 32 bit color space?
Thanks!
I looked up the method createScaledBitmap in the source for the Bitmap class (Link):
And the call to createBitmap() should return your unchanged source bitmap due to this check in the method body:
Looking at just this it would seem that your original bitmap is returned, But, if your bitmap happens to be mutable, you effectively skip this check and end up here:
As you are not performing any scaling, your matrix will be the identity matrix, and the condition is satisfied. The bitmap created is, as you can see, dependent on the alpha in the source bitmap. If no alpha is present, you end up with a result bitmap with the RGB_565 format rather than the ARGB_8888.
So, to scale and preserve the 32-bit format, your bitmap should either be immutable or use an Alpha channel.