Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to convert drawable to ImageVector?

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.

like image 616
Gohryt Avatar asked Dec 12 '25 13:12

Gohryt


2 Answers

Yes! ImageVector.vectorResource(R.drawable.ic_remove_circle)

it doesnt need Context.

like image 118
Z3R0 Avatar answered Dec 14 '25 14:12

Z3R0


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)
like image 23
Blunderer Avatar answered Dec 14 '25 12:12

Blunderer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!