Android: Lottie动画与动态文本和自定义字体问题



我试图在jetpack撰写LottieAnimation中动态交换文本。导出的lottie文件不带符号

当在

中使用旧的android视图时,它会工作
AndroidView(factory = { context ->
val view = LottieAnimationView(context).apply {
setAnimation(R.raw.testing_no_glyphs)
playAnimation()
repeatCount = LottieConstants.IterateForever
}
val textDel = object : TextDelegate(view) {
override fun getText(layerName: String?, input: String?): String {
return when (layerName) {
"Test234" -> "OtherLettersHere"
else -> super.getText(layerName, input)
}
}
}
val fontDel = object : FontAssetDelegate() {
override fun getFontPath(fontFamily: String?, fontStyle: String?, fontName: String?): String {
return "fonts/[MyFontInside /assets].ttf"
}
}
view.setTextDelegate(textDel)
view.setFontAssetDelegate(fontDel)
return@AndroidView view
})

但是我在JetpackCompose版本的Lottie中找不到正确的句柄来获得相同的结果。

如果我们导出带有字形的lottie,它适用于lottie json中chars数组中的字母。但是我们希望能够用任何字母/符号替换,所以这不是一个可行的解决方案。

我注意到在5.3.0-SNAPSHOT中添加了一个fontMap参数,但我不知道该按哪个键。

下面是我的代码:
val dynamicProperties = rememberLottieDynamicProperties(
rememberLottieDynamicProperty(LottieProperty.TEXT, value = "AaBbCcEeFf", keyPath = arrayOf("Test234"))
)
val composition by rememberLottieComposition(
spec = LottieCompositionSpec.RawRes(R.raw.testing)
)
val progress by animateLottieCompositionAsState(composition, iterations = LottieConstants.IterateForever)
LottieAnimation(
composition,
{ progress },
dynamicProperties = dynamicProperties,
fontMap = mapOf("fName" to Typeface.createFromAsset(LocalContext.current.assets, "fonts/[MyFontInside /assets].ttf"))
)

它只是为Lottie动画中的所有文本显示空白-所以这是我卡住的地方。

经过一番尝试,我发现了一种为特定图层添加字体的方法:

val typeface = Typeface.createFromAsset(LocalContext.current.assets, "fonts/[MyFontInside /assets].ttf")
val dynamicProperties = rememberLottieDynamicProperties(
rememberLottieDynamicProperty(LottieProperty.TEXT, value = "AaBbCcEeFf", keyPath = arrayOf("Test234")),
--> rememberLottieDynamicProperty(LottieProperty.TYPEFACE, value = typeface, keyPath = arrayOf("Test234")),
)

因此在我的案例中不需要fontMap

最新更新