Jetpack Compose 1.0.0-rc01中AnimatedImageVector及相关方法缺失,如何替代
Jetpack Compose 1.0.0-rc01 AnimatedImageVector 及相关方法替代方案
从Jetpack Compose 1.0.0-beta09版本开始,原AnimatedImageVector类和animatedVectorResource方法就已从Compose基础包中移除,迁移到了独立的animation-graphics库中,1.0.0-rc01版本延续了该调整,具体替代方案如下:
1. 新增依赖引入
在模块级build.gradle文件中添加animation-graphics依赖,版本需与项目内其他Compose依赖版本保持一致:
dependencies { implementation "androidx.compose.animation:animation-graphics:1.0.0-rc01" }
2. API调用替换
- 类引用替换:原
AnimatedImageVector替换为androidx.compose.animation.graphics.vector.AnimatedImageVector - 资源加载方法替换:原全局
animatedVectorResource()方法替换为AnimatedImageVector的同名静态方法,使用逻辑无感知,示例如下:
// 加载AVD资源 val avd = AnimatedImageVector.animatedVectorResource(R.drawable.your_animated_vector_drawable) // 配合动画状态使用示例 val isAtEnd = remember { mutableStateOf(false) } val painter = rememberAnimatedVectorPainter(avd, isAtEnd.value) Image( painter = painter, contentDescription = null ) // 触发动画执行 LaunchedEffect(isAtEnd.value) { animateTo(targetValue = !isAtEnd.value) }
该方案也适用于Compose 1.0.0正式版及后续所有稳定版本,无需二次调整。
内容的提问来源于stack exchange,提问作者Solvek
相关产品推荐
相关产品推荐

