Created
October 14, 2018 07:59
-
-
Save gowthamgts/9d496f42ce0acd16194641f69fcc48a6 to your computer and use it in GitHub Desktop.
A kotlin extension to convert a Drawable object 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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
awesome @gowthanmgts