Jetpack Compose 动画(基于动画矢量图像)

您可以通过几种不同的方式在 Compose 中为矢量添加动画效果。您可以使用以下任意一种方式:

  • AnimatedVectorDrawable 文件格式
  • ImageVector 与 Compose 动画 API 结合使用,如[这篇 Medium 文章] 中所述
  • 使用 Lottie 等第三方解决方案

带动画的矢量可绘制对象(实验性)

如需使用 AnimatedVectorDrawable 资源,请使用 animatedVectorResource 加载可绘制对象文件,并传入 boolean 以在可绘制对象的开始和结束状态之间切换,从而执行动画。

效果展示

需要先准备以下资源文件

res/drawable/animatorvectordrawable.xml

xml 复制代码
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/vectordrawable" >
    <target
        android:name="rotationGroup"
        android:animation="@animator/rotation" />
    <target
        android:name="v"
        android:animation="@animator/path_morph" />
</animated-vector>

res/drawable/vectordrawable.xml

xml 复制代码
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="64dp"
    android:width="64dp"
    android:viewportHeight="600"
    android:viewportWidth="600">
    <group
        android:name="rotationGroup"
        android:pivotX="300.0"
        android:pivotY="300.0"
        android:rotation="45.0" >
        <path
            android:name="v"
            android:fillColor="#000000"
            android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
    </group>
</vector>

res/animator/rotation.xml

xml 复制代码
<objectAnimator android:duration="6000"
    android:propertyName="rotation"
    android:valueFrom="0"
    android:valueTo="360"
    xmlns:android="http://schemas.android.com/apk/res/android" />

res/animator/path_morph.xml

xml 复制代码
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:duration="3000"
        android:propertyName="pathData"
        android:valueFrom="M300,70 l 0,-70 70,70 0,0   -70,70z"
        android:valueTo="M300,70 l 0,-70 70,0  0,140 -70,0 z"
        android:valueType="pathType" />
</set>

实现代码

ts 复制代码
@OptIn(ExperimentalAnimationGraphicsApi::class)
@Composable
private fun example1() {
    val image = AnimatedImageVector.animatedVectorResource(R.drawable.animatorvectordrawable)
  Column {
      customTitle(title = "带动画的矢量可绘制对象")

      val atEnd = remember { mutableStateOf(false) }
      Image(
          painter = rememberAnimatedVectorPainter(image, atEnd.value),
          contentDescription = "Timer",
          modifier = Modifier.fillMaxWidth().aspectRatio(1f).clickable {
              atEnd.value = !atEnd.value
          },
          contentScale = ContentScale.Crop
      )
      CodeView(assetUrl = "animation/animationValue/code1.txt", modifier = Modifier.padding(top = 10.dp))
  }
}
相关推荐
bytebeats17 小时前
Android 开发者的 Jetpack Compose 学习路线图
android·android jetpack
bytebeats17 小时前
Android 的未来: 为什么 `Navigation 3` 是行业变革者!
android·android jetpack
bytebeats17 小时前
如何在 Jetpack Compose 中创建逐字动画
android·android jetpack
柿蒂2 天前
Android图片批量添加处理优化:从「30」秒缩短至「4.4」秒
android·android jetpack
alexhilton2 天前
学会用最优雅的姿式在Compose中显示富文本
android·kotlin·android jetpack
我命由我123453 天前
Android Studio 提示信息 ‘equals(““)‘ can be replaced with ‘isEmpty()‘
android·ide·android studio·安卓·android jetpack·android-studio·android runtime
刘龙超3 天前
如何应对 Android 面试官 -> 运用 Jetpack 写一个音乐播放器(一)基础搭建
android jetpack
消失的旧时光-19435 天前
Android Jetpack 组件库 ->Jetpack Navigation (下)
android·android jetpack
ljt27249606615 天前
Compose笔记(三十八)--CompositionLocal
笔记·android jetpack
_一条咸鱼_6 天前
Android Runtime性能计数器实现深度剖析(95)
android·android jetpack