android lottie字体json,lottie-android

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值