如何在Compose中使用动态矢量图形?

4
我创建了矢量可绘制动画,并希望在Compose中使用。我在官方文档中发现需要调用animatedVectorResource,但是当前版本的Compose已经删除了整个AnimatedImageVector。如何在当前Compose版本中启动动画可绘制?
3个回答

4

您需要将Compose更新到1.1.0-alpha01版本,并按照最新的更改日志添加模块androidx.compose.animation:animation-graphics

enter image description here

implementation("androidx.compose.animation:animation-graphics:1.1.0-alpha01")

val image = animatedVectorResource(id = R.drawable.animated_vector)
val atEnd by remember { mutableStateOf(false) }
Icon(
   painter = image.painterFor(atEnd = atEnd),
   contentDescription = null
)

我需要类似于这个的东西 https://developer.android.com/reference/androidx/vectordrawable/graphics/drawable/SeekableAnimatedVectorDrawable,但是用于Compose? - Arsenius

3

AnimatedImageVector被暂时移除1.0.0-rc01中,并且在最终稳定版1.0.0中不存在。

1.1.0-alpha01开始,AnimatedImageVector和相关的API现在在新的androidx.compose.animation:animation-graphics模块中。

您可以使用类似以下的内容:

    val image = animatedVectorResource(drawableId)
    var atEnd by remember { mutableStateOf(false) }
    Image(
        painter = image.painterFor(atEnd),
        contentDescription = "Your content description",
        modifier = Modifier.size(64.dp).clickable {
            atEnd = !atEnd
        }
    )

0

更新的答案:参考资料

implementation("androidx.compose.animation:animation-graphics:1.3.3")

var atEnd by remember { mutableStateOf(false) }
val drawable = AnimatedImageVector.animatedVectorResource(R.drawable.avd_anim)

IconButton(onClick = { atEnd = !atEnd }) {
   Icon(
      painter = rememberAnimatedVectorPainter(animatedImageVector = drawable, atEnd = atEnd),
      contentDescription = null
   )
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接