8

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...

alain.janinm
  • 19,951
  • 10
  • 65
  • 112
Henry
  • 942
  • 3
  • 11
  • 21

4 Answers4

1

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.

Phileo99
  • 5,581
  • 2
  • 46
  • 54
  • You forgot the inScaled + inDensity + inTargetDensity method which does allow granular scaling. You just have to know the scaling magnitudes and work with inSampleSize. – Gensoukyou1337 Aug 11 '16 at 03:08
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.

Community
  • 1
  • 1
emmby
  • 99,783
  • 65
  • 191
  • 249
0

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.

Wroclai
  • 26,835
  • 7
  • 76
  • 67
0

Using Bitmap.createBitmap () becomes faster than using Bitmap.createScaledBitmap ().

Using Bitmap.createBitmap () we have already passed the Bitmap creation settings, while with Bitmap.createScaledBitmap () height and width are calculated dynamically.

See the example:

/**
 * Return a [Bitmap] representation of this [Drawable].
 *
 * If this instance is a [BitmapDrawable] and the [width], [height], and [config] match, the
 * underlying [Bitmap] instance will be returned directly. If any of those three properties differ
 * then a new [Bitmap] is created. For all other [Drawable] types, a new [Bitmap] is created.
 *
 * @param width Width of the desired bitmap. Defaults to [Drawable.getIntrinsicWidth].
 * @param height Height of the desired bitmap. Defaults to [Drawable.getIntrinsicHeight].
 * @param config Bitmap config of the desired bitmap. Null attempts to use the native config, if
 * any. Defaults to [Config.ARGB_8888] otherwise.
 */
fun Drawable.toBitmap(
    @Px width: Int = intrinsicWidth,
    @Px height: Int = intrinsicHeight,
    config: Config? = null
): Bitmap {
    if (this is BitmapDrawable) {
        if (config == null || bitmap.config == config) {
            // Fast-path to return original. Bitmap.createScaledBitmap will do this check, but it
            // involves allocation and two jumps into native code so we perform the check ourselves.
            if (width == intrinsicWidth && height == intrinsicHeight) {
                return bitmap
            }
            return Bitmap.createScaledBitmap(bitmap, width, height, true)
        }
    }

    val (oldLeft, oldTop, oldRight, oldBottom) = bounds

    val bitmap = Bitmap.createBitmap(width, height, config ?: Config.ARGB_8888)
    setBounds(0, 0, width, height)
    draw(Canvas(bitmap))

    setBounds(oldLeft, oldTop, oldRight, oldBottom)
    return bitmap
}

Source