Created
April 21, 2019 10:08
-
-
Save rezaiyan/08e46aa5ccb779ac463f6f1cc0b956ac to your computer and use it in GitHub Desktop.
A snippet code to do convert drawable to bitmap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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