单个LottieAnimationView加载不同的lottie动画

需求

最近做了一个单个LottieAnimationView根据用户点击选项的不同,加载不同的lottie动画的需求。网上的一些demo和博客大多只是单独的加载一个lottie动画,普遍不会有问题,但是如果加载不同的lottie动画时,遇到了一些问题,踩了一些坑,比如lottie动画只能播放第一个,后面的就不放了,比如第二个lottie动画播放时会闪一下第一个lottie动画画面,比如播放混乱,明明点击的是第一个动画,播放的确实第二个等等。特此把最后的使用总结如下。最后的效果因为涉及内部内容就不放了。

加载sdcard的lottie动画

 /**
     * 播放sdcard的动画
     * @param jsonFile	json文件
     * @param imagesDir json文件引用的image文件的目录
     * @throws Exception
     */
    private void  showSdcardLottieEffects(File jsonFile,File imagesDir) throws Exception{
        BufferedReader bufferedReader = new BufferedReader(new FileReader(jsonFile));
        String content = null;
        StringBuilder stringBuilder = new StringBuilder();
        while ((content = bufferedReader.readLine()) != null){
            stringBuilder.append(content);
        }

        JSONObject jsonObject = new JSONObject(stringBuilder.toString());
        final String absolutePath = imagesDir.getAbsolutePath();
        //提供一个代理接口从 SD 卡读取 images 下的图片
        specialEffectLottieAnim.setImageAssetDelegate(new ImageAssetDelegate() {
            @Override
            public Bitmap fetchBitmap(LottieImageAsset asset) {
                Bitmap bitmap = null;
                FileInputStream fileInputStream = null;
                try {
                    fileInputStream = new FileInputStream(absolutePath + File.separator + asset.getFileName());
                    bitmap = BitmapFactory.decodeStream(fileInputStream);
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    try {
                        if (bitmap == null) {
                            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8);
                        }
                        if (fileInputStream != null) {
                            fileInputStream.close();
                        }
                    } catch (IOException e) {
                        LogUtil.e(TAG, e);
                    }
                }
                return bitmap;
            }
        });

        LottieComposition.Factory.fromJson(getResources(), jsonObject, new OnCompositionLoadedListener() {
            @Override
            public void onCompositionLoaded(@Nullable LottieComposition composition) {
                if(composition == null){
                    return;
                }
                specialEffectLottieAnim.cancelAnimation();
                specialEffectLottieAnim.setProgress(0);
                specialEffectLottieAnim.setComposition(composition);
                specialEffectLottieAnim.playAnimation();
                specialEffectLottieAnim.setVisibility(View.VISIBLE);
            }
        });

    }
复制代码

加载应用内部assets目录下的lottie文件

/**
     * 从本地查找lottie动画
     * @param interactCode 特效name
     */
    private void showLocalLottieEffects(String interactCode){
        LogUtil.i(TAG, "启动本地动画 folderIsWatch:"+ folderIsWatch+ " interactCode:"+interactCode);
        try{
        //json文件的路径根据具体需求修改
            LottieComposition composition = LottieComposition.Factory.fromFileSync(this, "lottie/" +interactCode+".json");
            specialEffectLottieAnim.cancelAnimation();
            specialEffectLottieAnim.setProgress(0);
            specialEffectLottieAnim.setComposition(composition);
            specialEffectLottieAnim.playAnimation();
            specialEffectLottieAnim.setVisibility(View.VISIBLE);
        }catch (Exception e){
            LogUtil.i(TAG, "启动本地动画 "+"  interactCode:"+interactCode+"出错",e);
        }
    }
  
复制代码
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值