Skip to content

Instantly share code, notes, and snippets.

@shredderskelton
Created December 18, 2018 20:30
Show Gist options
  • Save shredderskelton/4c9dd013c480bf52c551561844296260 to your computer and use it in GitHub Desktop.
Save shredderskelton/4c9dd013c480bf52c551561844296260 to your computer and use it in GitHub Desktop.
Convert Vector Drawable to Bitmap
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