//资源打包成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 + i
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
t).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.GIFTPATH/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.GIFTPATH/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的走这里
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 “”
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)