将自定义字体设置为Lottie Android

0x6upsns  于 2022-12-25  发布在  Android
关注(0)|答案(1)|浏览(223)

假设我想使用此动画
https://lottiefiles.com/70883-animation-fade-in-fade-out
但更改字体自定义视图从我的项目。

上面的截图是我的字体文件夹。但当我运行应用程序,它给错误。

java.lang.RuntimeException: Font asset not found fonts/Arial.ttf
        at android.graphics.Typeface.createFromAsset(Typeface.java:965)
        at com.airbnb.lottie.manager.FontAssetManager.getFontFamily(FontAssetManager.java:87)
        at com.airbnb.lottie.manager.FontAssetManager.getTypeface(FontAssetManager.java:61)
        at com.airbnb.lottie.LottieDrawable.getTypeface(LottieDrawable.java:1094)
        at com.airbnb.lottie.model.layer.TextLayer.drawTextWithFont(TextLayer.java:238)
        at com.airbnb.lottie.model.layer.TextLayer.drawLayer(TextLayer.java:161)
        at com.airbnb.lottie.model.layer.BaseLayer.draw(BaseLayer.java:239)
        at com.airbnb.lottie.model.layer.CompositionLayer.drawLayer(CompositionLayer.java:120)
        at com.airbnb.lottie.model.layer.BaseLayer.draw(BaseLayer.java:239)
        at com.airbnb.lottie.LottieDrawable.drawWithOriginalAspectRatio(LottieDrawable.java:1247)
        at com.airbnb.lottie.LottieDrawable.drawInternal(LottieDrawable.java:393)
        at com.airbnb.lottie.LottieDrawable.draw(LottieDrawable.java:383)
        at android.widget.ImageView.onDraw(ImageView.java:1436)
        at android.view.View.draw(View.java:22350)
        at android.view.View.updateDisplayListIfDirty(View.java:21226)
        at android.view.View.draw(View.java:22081)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
        at android.view.View.draw(View.java:22353)
        at android.view.View.updateDisplayListIfDirty(View.java:21226)
        at android.view.View.draw(View.java:22081)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
        at android.view.View.updateDisplayListIfDirty(View.java:21217)
        at android.view.View.draw(View.java:22081)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
        at android.view.View.updateDisplayListIfDirty(View.java:21217)
        at android.view.View.draw(View.java:22081)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
        at android.view.View.updateDisplayListIfDirty(View.java:21217)
        at android.view.View.draw(View.java:22081)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
        at android.view.View.updateDisplayListIfDirty(View.java:21217)
        at android.view.View.draw(View.java:22081)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
        at android.view.View.draw(View.java:22353)
        at com.android.internal.policy.DecorView.draw(DecorView.java:806)
        at android.view.View.updateDisplayListIfDirty(View.java:21226)
        at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:559)
        at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:565)
        at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:642)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:4101)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3828)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3099)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
        at android.view.Choreographer.doCallbacks(Choreographer.java:796)
        at android.view.Choreographer.doFrame(Choreographer.java:731)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
2022-08-31 15:03:59.029 11213-11213/com.reeplayer.android.dev E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

所以这里的问题是,什么是正确的方式来设置自定义字体到lottie json文件

vojdkbi0

vojdkbi01#

您可以使用类似于以下内容的内容:

lottieAnimationView.setFontAssetDelegate(object : FontAssetDelegate() {
   override fun fetchFont(fontFamily: String): Typeface {
      return Typeface.createFromAsset(
         context.assets,
         "fonts/avertastd_regular.otf"
      )
   }
})

还要确保字体名称有效(小写,不含空格)。

相关问题