问题描述
我正在编写一个应用程序,并希望有一个抽奖动画作为我的启动画面,因为我在编写这个应用程序时正在学习 Java,所以我制作了一个测试应用程序来看看它是如何工作的。我发现几乎所有的乐天动画都显示为静止图像,并且不会播放/循环动画。我遵循了 guide on lottiefiles website 以及我在 other questions 上找到的信息,但我仍然无法播放动画。以下正是我添加的内容: 我的应用程序的 Gradle 依赖项:
dependencies {
implementation 'com.airbnb.android:lottie:3.6.1'
}
到我的布局 XML:
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/splashlottie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:lottie_fileName="opening_book.json" //Even tried placing the file in raw and using app:lottie_rawRes="@raw/opening_book"
app:lottie_autoplay="true"
app:lottie_loop="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
在我的活动中:
LottieAnimationView splashscreen;
splashscreen = findViewById(R.id.splashlottie);
splashscreen.animate();
splashscreen.playAnimation();
我不确定自己到底做错了什么,因为我什至尝试了各种不同的 Lottie 动画文件并将相同的文件放在 raw 和 assets 下。 API 级别会不会是动画无法播放的原因?
预先感谢您的帮助。
解决方法
我能够使用以下代码运行动画:
val imageView = view.findViewById<LottieAnimationView>(R.id.image_view)
imagView.playAnimation()
xml 代码:
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:lottie_fileName="womensDay.json"
app:layout_constraintBottom_toTopOf="@+id/button_first"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textview_first"
app:lottie_loop="true"/>
需要记住的几点:
- 确保您将 json 文件放在资产文件夹中,如果没有,请创建一个。参考:https://stackoverflow.com/a/50907775/5745574
- 只是为了测试,试着给你的视图绝对尺寸说 200dp(宽度和高度)。可能是您的视图没有合适的尺寸。
- 尝试为不同的 json 文件设置动画,您的文件可能已损坏
- 尝试将您的 lottieView 放在 ViewGroup(任何类型,例如 LinearLayout)中
- API 级别 16 及更高版本支持 Lottie 动画
看起来你做得对。是否有可能在您的手机上完全禁用动画?
,这是没有 lottieView.playAnimation() 因为 lottie_autoPlay="true":
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/anim_view"
android:layout_width="256dp"
android:layout_height="256dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:lottie_rawRes="@raw/anim_radio_waves"
app:lottie_autoPlay="true"
app:lottie_loop="true"/>
还要检查您的动画 json:lottie player