1.添加依赖
dependencies { implementation 'com.airbnb.android:lottie:2.5.5'//lottie }
2.1layout实现
<?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" android:layout_width="match_parent" android:layout_height="match_parent"> <com.airbnb.lottie.LottieAnimationView android:id="@+id/animation_view" android:layout_width="wrap_content" android:layout_height="wrap_content" app:lottie_fileName="motorcycle.json" app:lottie_autoPlay="true" app:lottie_loop="true" /> </RelativeLayout>
说明:
lottie_fileName是需要加载的动画 目录是:...\MyStudyApp\app\src\main\assets\motorycycle.json
lottie_autoPlay 是否自动播放
lottie_loop是否循环播放
以上两步即可现实动画效果
2.2使用java代码实现
LottieAnimationView animation_view;
animation_view = findViewById(R.id.animation_view); LottieComposition.Factory.fromAssetFileName(this, "happy.json", new OnCompositionLoadedListener() { @Override public void onCompositionLoaded(@Nullable LottieComposition composition) { animation_view.setComposition(composition); } });
3.资源
--motorcycle.json 、happy.json下载地址
--Lottie GitHub