最近一直在学习kotlin,又接触到 Lottie 动画,网上是Java写的 Lottie动画,我用kotlin写了一边,发现了一写问题。总结一下
Lottie,简单的说,流程就是(Android为例):UI设计给出动画的 Json 文件,放到项目中,加载,展示动画。
特别说明一下:Lottie 2.8.0,对应的最小SDK,一定要 大于等于16(Android 4.1)。现在很少有低于4.1了吧,注意下就好
我网上找了个动画对应的Json
出自文章:Lottie开源动画库介绍与使用示例
现在,开始使用:
这里,我先讲一下Java的使用,很简单:
1、我项目中,是用的 androidx
implementation "com.airbnb.android:lottie:2.8.0"
2、res文件夹下,创建raw文件夹,然后,创建一个:hello_world.json 文件
(上面提到的,学习文章中,有这么一句:建议使用 res/raw,因为可以对动画通过 R 文件使用静态引用,而不只是使用字符串名称)
3、把上面提到的,动画对应的 json,复制到 hello_world.json 文件中
4、布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.airbnb.lottie.LottieAnimationView
android:background="#55ff0000"
android:id="@+id/animation_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:lottie_loop="true"
app:lottie_rawRes="@raw/hello_world" />
</RelativeLayout>
5、代码中使用:
private