toBitmap

fun Drawable.toBitmap(@Px width: Int = intrinsicWidth, @Px height: Int = intrinsicHeight, config: Bitmap.Config? = null): Bitmap(source)

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.

Parameters

width

Width of the desired bitmap. Defaults to Drawable.getIntrinsicWidth.

height

Height of the desired bitmap. Defaults to Drawable.getIntrinsicHeight.

config

Bitmap config of the desired bitmap. Null attempts to use the native config, if any. Defaults to Config.ARGB_8888 otherwise.

See also

Throws

if the underlying drawable is a BitmapDrawable where BitmapDrawable.getBitmap returns null or the drawable cannot otherwise be represented as a bitmap