1.初始化本地zip文件
LottieCompositionFactory.fromZipStream(
ZipInputStream(FileInputStream("/storage/emulated/0/Android/data/packageName/files/test.zip")),
"test"
)
.addListener {
testLottieAnimationView.setComposition(it)
testLottieAnimationView.playAnimation()
}
2.加载本地字体资源
testLottieAnimationView.setFontAssetDelegate(object : FontAssetDelegate() {
override fun fetchFont(fontFamily: String?): Typeface {
return Typeface.createFromFile("/storage/emulated/0/Android/data/packageName/files/Hei.ttf")
}
override fun getFontPath(fontFamily: String?): String {
return "/storage/emulated/0/Android/data/packageName/files/Hei.ttf"
}
})
3.设置Lottie动画中的文本 替换对应的key
这个key是设计那边指定好的 具体是什么可以问一下
val textDelegate = TextDelegate(testLottieAnimationView)
textDelegate.setText("key", "要设置的文本")
4.设置Lottie动画中图片
testLottieAnimationView.updateBitmap( "key", bitmap)
其中bitmap可以自己转
上诉加载网络图片的时候会遇到一个崩溃问题
后续修改如下
private fun loadImage(url: String,key:String) {
Glide.with(this)
.asBitmap()
.override(110, 110)
.apply(RequestOptions.bitmapTransform(CircleCrop()))
.load(url)
.into(object:CustomTarget<Bitmap>(){
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
lottieAnimationView.addValueCallback(KeyPath(key), LottieProperty.IMAGE,object:LottieValueCallback<Bitmap>(){
override fun getValue(frameInfo: LottieFrameInfo<Bitmap>?): Bitmap {
return resource
}
})
}
override fun onLoadCleared(placeholder: Drawable?) {}
})
}
loadImage("url1","key1")
loadImage("url2","key2")
lottieStartPK.visibility = View.VISIBLE
lottieStartPK.playAnimation()