Lottie 5.2.0 简单使用

1.xml使用:

 <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/lt_gift"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        app:lottie_autoPlay="true"
        app:lottie_fileName="waveAnmim_gray.json"
        app:lottie_loop="true"
        app:lottie_colorFilter="@color/red"
        app:lottie_repeatMode="restart"/>

属性说明:

  1. lottie_autoPlay:自动播放动画(默认false)

  2. lottie_fileNameapp/src/main/assets目录下的json文件名(json文件可以直接放到assets目录下,比如“waveAnmim_gray.json”,也可新建子目录,比如"anmim/waveAnmim_gray.json")

  3. lottie_loop:循环播放(默认false)

  4. lottie_repeatMode:循环模式(默认restart)

2.代码实现

animationView = new LottieAnimationView(this);
animationView.setAnimation("waveAnmim_gray.json");
// 是否循环播放
animationView.setRepeatCount(LottieDrawable.INFINITE);
// 设置播放速率,例如:2代表播放速率是不设置时的二倍
animationView.setSpeed(1.5f);
animationView.setRepeatMode(LottieDrawable.REVERSE);
animationView.addValueCallback(new KeyPath("**", "矩形 1", "填充 1"), LottieProperty.COLOR_FILTER, new SimpleLottieValueCallback<ColorFilter>() {
       @Override
       public ColorFilter getValue(LottieFrameInfo<ColorFilter> frameInfo) {
             return new PorterDuffColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_ATOP);
            }
        });
 // 开始播放
 animationView.playAnimation();
        

通过resolveKeyPath可获取动画的所有keyPath。

animationView.resolveKeyPath(new KeyPath("**")).forEach(keyPath -> {
    QLog.i("*****************:" + keyPath);
});

打印:

*****************:KeyPath{keys=[形状图层 3, 矩形 1],resolved=true}
*****************:KeyPath{keys=[形状图层 3, 矩形 1, 填充 1],resolved=true}
*****************:KeyPath{keys=[形状图层 15],resolved=true}
*****************:KeyPath{keys=[形状图层 15, 矩形 1],resolved=true}
*****************:KeyPath{keys=[形状图层 15, 矩形 1, 填充 1],resolved=true}
*****************:KeyPath{keys=[形状图层 2],resolved=true}
*****************:KeyPath{keys=[形状图层 2, 矩形 1],resolved=true}
*****************:KeyPath{keys=[形状图层 2, 矩形 1, 填充 1],resolved=true}
*****************:KeyPath{keys=[形状图层 1],resolved=true}
*****************:KeyPath{keys=[形状图层 1, 矩形 1],resolved=true}
*****************:KeyPath{keys=[形状图层 1, 矩形 1, 填充 1],resolved=true}

.......

 通过keyPath可改变图层颜色

// 改变 形状图层 1, 矩形 1, 填充 1 这一个图层的颜色
animationView.addValueCallback(new KeyPath("形状图层 1", "矩形 1", "填充 1"), LottieProperty.COLOR_FILTER, new SimpleLottieValueCallback<ColorFilter>() {
    @Override
    public ColorFilter getValue(LottieFrameInfo<ColorFilter> frameInfo) {
        return new PorterDuffColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_ATOP);
    }
});

// 想要改变多个图层颜色,可使用通配符**;

// 以下为匹配 矩形 1, 填充 1 的所有图层都改变颜色。

animationView.addValueCallback(new KeyPath("**", "矩形 1", "填充 1"), LottieProperty.COLOR_FILTER, new SimpleLottieValueCallback<ColorFilter>() {
    @Override
    public ColorFilter getValue(LottieFrameInfo<ColorFilter> frameInfo) {
        return new PorterDuffColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_ATOP);
    }
});

改变颜色前:

改变颜色后:

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值