difference between methods to scale a bitmap

2019-04-23 07:51发布

问题:

There are at least two methods to scale a bitmap in Android, One is to use "inScaled, inDensity, inTargetDensity" in "BitmapFactory.Options" when decode a bitmap source. The other is to use a "Matrix" in "Bitmap.createBitmap".

What I am curious is what the difference between these two method is? What about the quality of produced bitmap? And what about the memory usage? etc...

回答1:

Using BitmapFactory with the proper inScale options will allow you to be more memory efficient than using either Bitmap.createScaledBitmap() or Bitmap.createBitmap() with a matrix scale. However, it is more complicated.

Check out How do I scale a streaming bitmap in-place without reading the whole image first? for details.



回答2:

There is no "big" difference. Although, one benefit with using the BitmapFactory.Options is that you can check for the width / height of your Bitmap without allocating memory for the actual Bitmap's pixels.

Also, you can easily see which options BitmapFactory.Options has and compare that with Bitmap.createBitmap(). In general, BitmapFactory.Options is like a "tool" API for making decoding and sampling Bitmaps easier.



回答3:

One difference that I found was that using BitmapFactory`s options.inSampleSize to scale bitmaps is not as granular, because the scale will be 1/inSampleSize, and because inSampleSize must be an integral number, you wind up with scaling like 1/2, 1/3, 1/4, etc, but nothing more granular than that.

Bitmap.createScaledBitmap(), though more memory intensive, allows more granular scaling, up to 1dp resolution.