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

huangapple go评论75阅读模式
英文:

Android: Lottie Animation with dynamic text and custom font issue

问题

我试图在Jetpack Compose中动态替换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
})

但我无法找到在Jetpack Compose版本的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动画内所有文本的空白 - 这就是我卡住的地方。

英文:

I'm trying to dynamically swap a text inside a LottieAnimation in jetpack compose.
The lottie file is exported without glyphs

It's working when using the old android view inside a

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
})

But I can't find the correct handles in the JetpackCompose version of Lottie to get the same result.

If we export the lottie with glyphs, it's works for the letters in the chars array inside the lottie json. But we want to be able to replace with any letters/symbols, so this isn't a viable solution.

I've noticed in the 5.3.0-SNAPSHOT that a fontMap parameter has been added, but I can't figure out which key to hit with it.

Here is my code:

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"))
)

It just shows a blank for all the texts inside the Lottie Animation - so this is kinda where i'm stuck.

答案1

得分: 0

经过一些尝试和错误,我找到了一种为特定图层添加字体的方法:

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

英文:

After some trial an error I found a way to add the typeface for a specific layer:

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")),
)

Hence there is no need for the fontMap in my case

huangapple
  • 本文由 发表于 2023年1月9日 16:48:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定