该问题的根本原因在于使用的Android studio的版本过低,2023年Lottie 3.0和Bodymovin 5.5有一些重要的json优化,可以节省json大小和解析速度的1/3。 但是,必须在3.0以上生效,所以需要下载3.0版本以上的Android studio才可以使用Lottie的json格式动画
下载地址:下载 Android Studio 和应用工具 - Android 开发者 | Android Developers (google.cn)
但本人因习惯使用旧版本的Android,不愿意下载最新版本,所以最后选择下载git格式的Lottie动画,通过android-gif-drawable加载。
activity.xml界面代码参考如下:
<pl.droidsonroids.gif.GifImageView
android:id="@+id/lottie"
android:src="@raw/dong"
android:layout_width="wrap_content"
android:layout_height="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
Java界面代码参考如下:
GifImageView gifImageView = findViewById(R.id.lottie);
GifDrawable gifDrawable = (GifDrawable) gifImageView.getDrawable();
gifDrawable.setLoopCount(1);//GIF动画循环次数
完成后动画运行成功