Skip to content

Instantly share code, notes, and snippets.

@gowthamgts
Created October 14, 2018 07:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gowthamgts/9d496f42ce0acd16194641f69fcc48a6 to your computer and use it in GitHub Desktop.
Save gowthamgts/9d496f42ce0acd16194641f69fcc48a6 to your computer and use it in GitHub Desktop.
A kotlin extension to convert a Drawable object to Bitmap
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
fun Drawable.toBitmap(): Bitmap {
if (this is BitmapDrawable) {
return this.bitmap
}
val bitmap = Bitmap.createBitmap(this.intrinsicWidth, this.intrinsicHeight, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
this.setBounds(0, 0, canvas.width, canvas.height)
this.draw(canvas)
return bitmap
}
@mochadwi
Copy link

awesome @gowthanmgts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment