有什么方法可以将drawable转换为ImageVector吗?

Goh*_*ryt 9 android android-jetpack-compose

我的意思是使用 vectorResource(id = ...) 以外的方法(这让我对上下文、资源和其他事物的不断调用感到困惑)。

我问这个是因为我发现我们可以在不使用资源的情况下制作文本样式、形状、颜色和字符串。

作为例子iconAccent = Color(context.getColor(R.color.iconAccent))

编辑1:
我发现 Bitmap.asImageAsset() 方法,它在我的情况下不起作用,因为矢量,但可能对某人有用。

编辑2:
我通过这种方式得到结果:

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

DrawableCompat.setTint需要使用主题颜色正确绘制图标(我们不需要它,因为它正确地从属性vectorResource(id = ...)中获取颜色)fillColor

但这是拐杖,如果你知道任何其他方法 - 请写下来。相信compose开发者会考虑到这个问题。

小智 2

是的! ImageVector.vectorResource(R.drawable.ic_remove_circle)

它不需要上下文。