Last active
June 10, 2020 17:39
-
-
Save mdavalos1993/ce88b657fd9c4419c4b75adb3bc32659 to your computer and use it in GitHub Desktop.
Convert any view to bitmap in Android Kotlin
This file contains hidden or 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
package me.marcosdavalos.util | |
import android.graphics.Bitmap | |
import android.graphics.Canvas | |
import android.graphics.Color | |
import android.view.View | |
val View.toBitmap: Bitmap get(){ | |
val bitmap = Bitmap.createBitmap(this.width, this.height, Bitmap.Config.ARGB_8888) | |
val canvas = Canvas(bitmap) | |
val bgDrawable = this.background | |
if(bgDrawable != null){ | |
bgDrawable.draw(canvas) | |
}else{ | |
canvas.drawColor(Color.BLACK) | |
} | |
this.draw(canvas) | |
return bitmap | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment