android lottie字体json,lottie-android

毋弘光
2023-12-01

build.gradlefile:dependencies {

compile 'com.airbnb.android:lottie:1.0.1'

}

lottie 支持 Jellybean (API 16) 及以上。最简单的使用方式是和LottieAnimationView一起使用:

android:id="@+id/animation_view"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

app:lottie_fileName="hello-world.json"

app:lottie_loop="true"

app:lottie_autoPlay="true" />

你可以在代码里动态的加载。加载app/src/main/assets中的json:LottieAnimationView animationView = (LottieAnimationView) findViewById(R.id.animation_view);

animationView.setAnimation("hello-world.json");

animationView.loop(true);

这个方法将加载文件并在后台解析动画,解析完成即开始异步渲染。

如果你想复用动画比如列表的每个item中或者从网络请求一个JSONObject:LottieAnimationView animationView = (LottieAnimationView) findViewById(R.id.animation_view);

...

LottieComposition composition = LottieComposition.fromJson(getResources(), jsonObject, (composition) -> {

animationView.setComposition(composition);

animationView.playAnimation();

});

然后你就可以控制动画并添加listener了:animationView.addAnimatorUpdateListener((animation) -> {

// Do something.

});

animationView.playAnimation();

...

if (animationView.isAnimating()) {

// Do something.

}

...

animationView.setProgress(0.5f);

...

// Custom animation speed or duration.

ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f)

.setDuration(500);

animator.addUpdateListener(animation -> {

animationView.setProgress(animation.getAnimatedValue());

});

animator.start();

...

animationView.cancelAnimation();

在底层LottieAnimationView使用LottieDrawable来渲染动画,如果你需要,你可以直接使用drawable:LottieDrawable drawable = new LottieDrawable();

LottieComposition.fromAssetFileName(getContext(), "hello-world.json", (composition) -> {

drawable.setComposition(composition);

});

如果你的动画被频繁使用,LottieAnimationView有一个可选的缓存策略:LottieAnimationView#setAnimation(String, CacheStrategy)。CacheStrategy可以是 Strong, Weak, 或者 None

 类似资料: