Skip to content

Instantly share code, notes, and snippets.

@rezaiyan
Created April 21, 2019 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rezaiyan/08e46aa5ccb779ac463f6f1cc0b956ac to your computer and use it in GitHub Desktop.
Save rezaiyan/08e46aa5ccb779ac463f6f1cc0b956ac to your computer and use it in GitHub Desktop.
A snippet code to do convert drawable to bitmap
fun Drawable.toBitmap(): Bitmap {
val bitmap = if (this.intrinsicWidth <= 0 || this.intrinsicHeight <= 0) {
Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888) // Single color bitmap will be created of 1x1 pixel
} else {
Bitmap.createBitmap(this.intrinsicWidth, this.intrinsicHeight, Bitmap.Config.ARGB_8888)
}
if (this is BitmapDrawable) {
if (this.bitmap != null) {
return this.bitmap
}
}
val canvas = Canvas(bitmap)
this.setBounds(0, 0, canvas.width, canvas.height)
this.draw(canvas)
return bitmap
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment