Android 使用 Lottie 库简单快速地实现漂亮的动画
Lottie 是 Airbnb 开发的一款能够为原生应用添加动画效果的开源工具,能够实时渲染 After Effects 动画特效。
安卓实现动画的方式有很多种,Lottie 库提供了更加简便的方法,资源也更加轻量,如果使用 Json 格式的动画资源,很多动画资源的大小就几 KB 。
Github 开源地址:https://github.com/airbnb/lottie-android
添加依赖
在 app/build.gradle 下添加:
dependencies {
implementation 'com.airbnb.android:lottie:3.4.1'
...
}
注意:Lottie 最低支持 API 16
使用
在要使用 Lottie 的布局文件下添加:com.airbnb.lottie.LottieAnimationView
,这里演示的是 ConstraintLayout
内加一个 com.airbnb.lottie.LottieAnimationView
并给 com.airbnb.lottie.LottieAnimationView
一个 id 为 lottieAnimationView。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=