Created
December 18, 2018 20:30
-
-
Save shredderskelton/4c9dd013c480bf52c551561844296260 to your computer and use it in GitHub Desktop.
Convert Vector Drawable to Bitmap
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
R.drawable.my_vector_drawable.toBitmap(requireContext(), R.color.onSurface) | |
fun Int.toBitmap(context: Context, @ColorRes tintColor: Int? = null): Bitmap? { | |
// retrieve the actual drawable | |
val drawable = ContextCompat.getDrawable(context, this) ?: return null | |
drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight) | |
val bm = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888) | |
// add the tint if it exists | |
tintColor?.let { | |
DrawableCompat.setTint(drawable, ContextCompat.getColor(context, it)) | |
} | |
// draw it onto the bitmap | |
val canvas = Canvas(bm) | |
drawable.draw(canvas) | |
return bm | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment