ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-09-05 22:30:57  阅读:336  来源: 互联网

标签:动画 return FileDownloadConstant val fis lottieAnimationView 服务器 Android 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.icode9.com/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)**

标签:动画,return,FileDownloadConstant,val,fis,lottieAnimationView,服务器,Android,Lottie
来源: https://blog.csdn.net/m0_61418455/article/details/120122889

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有