Lottie动画资源放到服务器预下载
创始人
2025-01-16 22:04:55
0

object FileDownloadConstant {
const val filePath = “gifts/zip”
//下载下来的资源存放的根目录
var DEFAULT_FILE_CACHE_PATH = ContextUtils.getApplicationContext().getExternalFilesDir(“gift”).path
//礼物动画存储目录
var rootPathAnimation = “ D E F A U L T F I L E C A C H E P A T H / {DEFAULT_FILE_CACHE_PATH}/ DEFAULTF​ILEC​ACHEP​ATH/{filePath}”

object BaseConfig{
//资源存放的apk地址
const val ANIMATION_OSS_URL = “https://static.network.com”
const val GIFT_PATH = “ A N I M A T I O N O S S U R L / {ANIMATION_OSS_URL}/ ANIMATIONO​SSU​RL/{filePath}”
}

//资源列表
var giftList = listOf(“xiagujian”,“chiji”,“kele”,“banzhuan”,“memeda”,“qiujiaowang”,“qiqiu”,“penghua”,
“yanhua”,“meigui”,“feiji”,“chengbao”,“bixin”,“loveyou”,“sobuteful”,“hongchun”,“girlfri”,“huangguan”,
“leihen”,“qiubite”,“haozhuchi”,“zhuguang”,“langman”,“bidong”,“dangao”,“laohu”,“mao”,“xiong”,“hunsha”,
“dianzan”,“bangbangtang”,“icecream”,“yiwendingqing”,“haidao”,“iphone”,“yecanyuehui”,“lovetree”,“jiarilvxing”)
}

文件下载工具类

/**

  • 文件下载工具类
    */
    object FileDownloadUtil {
    private val parentPath = FileDownloadConstant.rootPathAnimation
    private val fetch = Fetch.getInstance(FetchConfiguration.Builder(ContextUtils.getApplicationContext())
    .setLogger(FetchLogger(true,“动画下载”))
    .enableLogging(true)
    .setDownloadConcurrentLimit(5)
    .setAutoRetryMaxAttempts(3)
    .setHttpDownloader(OkHttpDownloader(Downloader.FileDownloaderType.PARALLEL))
    .setProgressReportingInterval(1000)
    .build())

/**

  • 下载监听
    */
    class DownloadListener(private val groupId : Int,private val num:Int,private val callback 😦(Boolean) -> Unit) ?) : AbstractFetchListener(){
    private var downloaded: Int = 0 //已下载的数量
    override fun onCompleted(download: Download) {
    if (download.group != groupId) {
    return
    }
    //资源打包成zip文件放到了服务器上,所以下载下来要解压缩
    LottieAnimationUtils.unZip(download.file, parentPath)
    downloadCount(true)
    }

override fun onError(download: Download, error: Error, throwable: Throwable?) {
if (download.group != groupId) {
return
}
downloadCount(false)
}

private val downloadCount: (Boolean) -> Unit = {
downloaded++
if (downloaded == num) {
callback?.invoke(it)
fetch.removeListener(this)
}
}
}

fun downloadList(pathList: List, priority: Priority = Priority.NORMAL, callback: ((Boolean) -> Unit)? = null) {
val groupId = Random.nextInt()
pathList.filter {
//本地文件不存在的才下载
!File(parentPath + File.separator + it).exists()
}.flatMap { path ->
arrayListOf(Request(“ F i l e D o w n l o a d C o n s t a n t . B a s e C o n f i g . G I F T P A T H / {FileDownloadConstant.BaseConfig.GIFT_PATH}/ FileDownloadConstant.BaseConfig.GIFTP​ATH/path” + “.zip”, parentPath + “/” + path + “.zip”).also {
it.groupId = groupId
it.priority = priority
})
}.apply {
if (size == 0) {
callback?.invoke(true)
return@apply
}
fetch.addListener(DownloadListener(groupId, size, callback)).enqueue(this)
}
}

/**

  • 下载单个文件
    */
    fun download(path: String, callback: ((Boolean) -> Unit)? = null) {
    if (File(“ p a r e n t P a t h / parentPath/ parentPath/path” + “.zip”).exists()) {
    callback?.invoke(true)
    return
    }
    val groupId = Random.nextInt()
    fetch.addListener(DownloadListener(groupId, 1, callback)).enqueue(Request(“ F i l e D o w n l o a d C o n s t a n t . B a s e C o n f i g . G I F T P A T H / {FileDownloadConstant.BaseConfig.GIFT_PATH}/ FileDownloadConstant.BaseConfig.GIFTP​ATH/path” + “.zip”, “ p a r e n t P a t h / parentPath/ 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的走这里

结尾

我还总结出了互联网公司Android程序员面试涉及到的绝大部分面试题及答案,并整理做成了文档,以及系统的进阶学习视频资料分享给大家。
(包括Java在Android开发中应用、APP框架知识体系、高级UI、全方位性能调优,NDK开发,音视频技术,人工智能技术,跨平台技术等技术资料),希望能帮助到你面试前的复习,且找到一个好的工作,也节省大家在网上搜索资料的时间来学习。

image
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
搜索资料的时间来学习。**

[外链图片转存中…(img-NGrXGGWu-1714771521859)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

相关内容

热门资讯

总结了解!透视辅助功能插件下载... 总结了解!透视辅助功能插件下载(辅助)大宝江苏麻将真是是有辅助修改器(哔哩哔哩)1、透视辅助功能插件...
曝光了解!多乐跑如何增加胜率(... 曝光了解!多乐跑如何增加胜率(辅助)微乐长沙麻将都是是有辅助app(哔哩哔哩)1、打开软件启动之后找...
专业了解!敢拍小程序靠谱吗(辅... 专业了解!敢拍小程序靠谱吗(辅助)边锋老友二打一竟然是真的辅助脚本(哔哩哔哩)1、下载好敢拍小程序靠...
教你了解!雀友会潮汕辅助(辅助... 教你了解!雀友会潮汕辅助(辅助)吉祥麻将原来是有辅助app(哔哩哔哩)1、进入到雀友会潮汕辅助是否有...
辅助了解!全来潜山跑风破解版安... 辅助了解!全来潜山跑风破解版安卓(辅助)山东麻将确实是有辅助安装(哔哩哔哩)1、下载好全来潜山跑风破...
解谜了解!微信小程序哥哥打大a... 解谜了解!微信小程序哥哥打大a辅助器(辅助)圣盛晃晃麻将切实有挂辅助安装(哔哩哔哩)1、微信小程序哥...
关于了解!黑桃a3哟西破解版(... 关于了解!黑桃a3哟西破解版(辅助)飘飘切实是真的辅助器(哔哩哔哩)在进入黑桃a3哟西破解版软件靠谱...
透视了解!哈局八张透视辅助(辅... 透视了解!哈局八张透视辅助(辅助)荔枝竞技都是真的有辅助软件(哔哩哔哩)1、让任何用户在无需哈局八张...
开挂了解!天酷辅助器(辅助)欢... 开挂了解!天酷辅助器(辅助)欢喜510k切实真的是有辅助下载(哔哩哔哩)1、天酷辅助器透视辅助软件激...
分享了解!新老夫子较二八年(辅... 分享了解!新老夫子较二八年(辅助)雀友会2广东潮汕麻将都是真的是有辅助app(哔哩哔哩)新老夫子较二...