Lottie动画资源放到服务器预下载,2024金九银十Android大厂面试题来袭

本文介绍了如何使用Fetch框架在Android应用中预下载Lottie动画资源,通过服务器上的zip文件进行下载和解压缩。文章还提供了一段下载监听和管理下载任务的代码示例,并在启动页调用下载方法。此外,作者分享了个人的开发经验,并提供了Android开发学习资料和面试知识点的总结。
摘要由CSDN通过智能技术生成

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}/ F

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值