1.依赖
在项目的 build.gradle 文件添加依赖
dependencies {
compile ‘com.airbnb.android:lottie:2.1.0’
}
2.动画文件
第一种方法,将我们所需要的动画文件loading.json保存在app/src/main/assets文件里。 第二种方法,网络上AE生成的动画文件。(网络链接)
3.使用
在布局文件中使用
<com.airbnb.lottie.LottieAnimationView
android:id=“@+id/animation_view”
android:layout_width=“400dp”
android:layout_height=“400dp”
app:lottie_fileName=“loading.json”
app:lottie_loop=“true”
app:lottie_autoPlay=“true”/>
使用网络加载AE生成的动画文件json
private void loadUrl(String url) {
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
try {
JSONObject json = new JSONObject(response.body().string());
LottieComposition.Factory
.fromJson(getResources(), json, new OnCompositionLoadedListener() {
@Override
public void onCompositionLoaded(LottieComposition composition) {
setComposition(composition);
}
});
} catch (JSONException e) {
}
}
}
});
}
private void setComposition(LottieComposition composition){
animation_view.setProgress(0);
animation_view.loop(true);
a