Lottie动画资源放到服务器预下载,一文说清

2021/9/5 23:11:29

本文主要是介绍Lottie动画资源放到服务器预下载,一文说清,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

             callback?.invoke(true)
            return@apply
        }
        fetch.addListener(DownloadListener(groupId, size, callback)).enqueue(this)
    }
}

/**
 * 下载单个文件
 */
fun download(path: String, callback: ((Boolean) -> Unit)? = null) {
    if (File("$parentPath/$path" + ".zip").exists()) {
        callback?.invoke(true)
        return
    }
    val groupId = Random.nextInt()
    fetch.addListener(DownloadListener(groupId, 1, callback)).enqueue(Request("${FileDownloadConstant.BaseConfig.GIFT_PATH}/$path" + ".zip", "$parentPath/$path.zip").also { it.groupId = groupId })
}

}

下载框架使用的是一个开源的,名字叫Fetch

在启动页的onCreate方法中,调用:

FileDownloadUtil.INSTANCE.downloadList(FileDownloadConstant.INSTANCE.getGiftList(), Priority.NORMAL,null);

就开始下载资源了

那么怎么使用Lottie框架加载文件夹中的动画资源呢?直接上代码

object LottieAnimationUtils {
//lottie加载本地资源的核心方法
fun loadLottie(lottieAnimationView: LottieAnimationView,imagePath:String,dataPath : String,giftName:String):FileInputStream?{
lottieAnimationView.setCacheComposition(false)
val imageFiles = File(imagePath)
val jsonFile = File(dataPath)
var fis:FileInputStream? = null
//资源中只有json文件的时候走这里
if (!imageFiles.exists()) {
if (jsonFile.exists()) {
fis = FileInputStream(jsonFile)
try {
lottieAnimationView.setImageAssetDelegate(null)
}catch (e : Exception) {

            }
            LottieComposition.Factory.fromInputStream(fis) {
                lottieAnimationView.postDelayed(Runnable {
                    lottieAnimationView.visibility = View.VISIBLE
                },200)
                it?.let {
                    it1 -> lottieAnimationView.setComposition(it1)
                }
                lottieAnimationView.playAnimation()
            }
        }else {
            FileDownloadUtil.download(giftName)
        }
        return fis
    }
    //资源中有imges和json的走这里
    try {
        fis = FileInputStream(jsonFile)
        val absolutePath = imageFiles.absolutePath
        lottieAnimationView.setImageAssetDelegate() {
            val opts = BitmapFactory.Options()
            opts.inScaled = true
            var bitmap: Bitmap? = null
            bitmap = BitmapFactory.decodeFile(absolutePath + File.separator + it.fileName, opts)
            bitmap
        }
        LottieComposition.Factory.fromInputStream(fis) {
            lottieAnimationView.postDelayed(Runnable {
                lottieAnimationView.visibility = View.VISIBLE
            },200)
            it?.let { it1 -> lottieAnimationView.setComposition(it1) }
            lottieAnimationView.playAnimation()
        }
        return fis
    } catch (e: Exception) {

    }
    return null
  }
  
  //这个是解压缩的方法,解压缩的工具类可以从网上找一个
  fun unZip(imagePath: String,destPath:String) {
     val file = File(imagePath)
     val destFile = File(destPath)
     if (!destFile.exists()) {
        destFile.mkdir()
     }
     if (!file.exists()) return
     ZipUtils.unzipFile(file,destFile)
  }
  
  //辅助类,不重要
  fun inputStreamClose(fis:FileInputStream?) {
    if (fis != null) {
        try {
            fis.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
     }
  }
//辅助类,不重要
fun getGiftFileName(giftId:Int):String {
    if (giftId > FileDownloadConstant.giftList.size){
        return ""
    }
    return FileDownloadConstant.giftList[giftId?.minus(1)]
}

}

使用方式:

val giftFileName = LottieAnimationUtils.getGiftFileName(actionAttachment.id)
val imagePath = “ F i l e D o w n l o a d C o n s t a n t . r o o t P a t h A n i m a t i o n / {FileDownloadConstant.rootPathAnimation}/ FileDownloadConstant.rootPathAnimation/giftFileName/images”
val jsonPath = “ F i l e D o w n l o a d C o n s t a n t . r o o t P a t h A n i m a t i o n / {FileDownloadConstant.rootPathAnimation}/ FileDownloadConstant.rootPathAnimation/giftFileName/data.json”
fis = loadLottie(lottie_likeanim, imagePath, jsonPath,giftFileName)

大概使用方式就是这样的,以上就是一些伪代码,但是基本原理就是这样

### 题外话


我们见过很多技术leader在面试的时候,遇到处于迷茫期的大龄程序员,比面试官年龄都大。这些人有一些共同特征:可能工作了7、8年,还是每天重复给业务部门写代码,工作内容的重复性比较高,没有什么技术含量的工作。问到这些人的职业规划时,他们也没有太多想法。

其实30岁到40岁是一个人职业发展的黄金阶段,一定要在业务范围内的扩张,技术广度和深度提升上有自己的计划,才有助于在职业发展上有持续的发展路径,而不至于停滞不前。

不断奔跑,你就知道学习的意义所在!


> **注意:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)**

![](https://www.www.zyiz.net/i/ll/?i=img_convert/4751e4d1c7abe41f7e66927ee4fbee1c.png)

小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)**

[外链图片转存中...(img-M3Rq74BK-1630852040865)]

**[CodeChina开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》](https://codechina.csdn.net/m0_60958482/android_p7)**


这篇关于Lottie动画资源放到服务器预下载,一文说清的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程