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

jdgnovmf  于 2023-10-14  发布在  Android
关注(0)|答案(2)|浏览(272)

我试图在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
})

但是我在Lottie的JetpackCompose版本中找不到正确的句柄来获得相同的结果。
如果我们导出带有字形的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"))
)

它只是显示了一个空白的所有文本内洛蒂动画-所以这是有点我卡住了。

yeotifhr

yeotifhr1#

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

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

b1zrtrql

b1zrtrql2#

您需要在LottieAnimation中使用fontMap字段。在Lottie json文件中,您需要检查它需要什么字体系列,并提供到Typeface的Map

  • fontMap:密钥可以是Lottie文件中指定的“fName”、“fFamily”或“fFamily-fStyle”。*
val customFontFamily = FontFamily(Font(R.font.roboto))
val resolver = LocalFontFamilyResolver.current
val typeface by remember { 

LottieAnimation(
    ...
    fontMap = mapOf(
        "Roboto-Regular" to typeface.value as Typeface
    ),
)

val customFontFamily = FontFamily(Font(R.font.roboto))
val resolver = LocalFontFamilyResolver.current
val typeface by remember { mutableStateOf(resolver.resolve(customFontFamily)) }

val dynamicProperties = rememberLottieDynamicProperties(
    rememberLottieDynamicProperty(
        property = LottieProperty.TYPEFACE,
        value = typeface.value,
        keyPath = arrayOf(
            "**" // or some other key path
        )
    )
)

相关问题