Is there any way to convert drawable to ImageVector?

Viewed 793

I mean ways other than using vectorResource(id = ...) (this confuses me with constant calls to context, resources, and other things).

I ask this because i see that we can make TextStyles, Shapes, Colors and Strings without smthngResource using.

As example iconAccent = Color(context.getColor(R.color.iconAccent))

Edit 1:
I found Bitmap.asImageAsset() method, it doesn't work in my case because vectors, but may be useful for somebody.

Edit 2:
I get the result by this way:

val group = ContextCompat.getDrawable(context, R.drawable.icon_group) as VectorDrawable
DrawableCompat.setTint(group, context.getColor(R.color.foregroundMain))
group = group.toBitmap().asImageBitmap()

DrawableCompat.setTint needed to correctly paint icons with theme color (we don't need it with vectorResource(id = ...) because it correctly take colors from fillColor property)

But it's crutch and if you know any other way - please write it. Believe compose developers will take into consideration this problem.

1 Answers

You can use this tool (Svg2Compose : https://github.com/DenisMondon/Svg2Compose) to convert a Vector Drawable into an ImageVector.
So that you will be able to call directly:

Icons.YOUR_ICON

instead of:

vectorResource(R.drawable.your_icon)
Related