【问题标题】:Lottie animation paddingLottie 动画填充
【发布时间】:2019-01-04 07:18:29
【问题描述】:

对于有使用 lottie json 文件经验的人,我有一个问题。我使用lottie 的经验并不多,所以我想也许我缺少一些明显的知识。当我将动画渲染到视图中时,动画对象会像这样放置在视图的中心

 _____________________________________________
|                                             |
|        [animated object]                    |
|_____________________________________________|

有没有办法可以修改 json 文件以使动画对象像这样适合整个视图:

     _____________________________________________
    |                                             |
    | [a n i m a t e d         o b j e c t s     ]|
    |_____________________________________________|

我曾尝试像这样在 xml 中设置视图:

android:scaleType="centerCrop"
android:adjustViewBounds="true"

但它没有工作
我还尝试设置更大的比例:

app:lottie_scale="2" 

但我没有成功。 感谢您的宝贵时间!

【问题讨论】:

    标签: android json animation padding lottie


    【解决方案1】:

    使用“LottieDrawable”和“LottieComposition”,您可以轻松设置比例。波纹管代码 100% 为我工作。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
    
    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/animation_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:lottie_loop="true"
        app:lottie_autoPlay="true"/>
    
    </LinearLayout>
    

    和Java部分

    LottieAnimationView animationView = findViewById(R.id.animation_view);
    
    LottieDrawable drawable = new LottieDrawable();
    
        LottieComposition.Factory.fromAssetFileName(this, "anim_1.json",(composition -> {
            drawable.setComposition(composition);
            drawable.playAnimation();
            drawable.setScale(3);
            animationView.setImageDrawable(drawable);
    
        }));
    

    【讨论】:

    • 你用的是什么版本?这似乎对我不起作用
    【解决方案2】:

    只需像对待任何其他视图一样对待它,并以编程方式使用 setPadding。

    LottieAnimationView btnHeart;
    
    btnHeart.setPadding(-150, -150, -150, -150);
    

    【讨论】:

      【解决方案3】:
      <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
            <com.airbnb.lottie.LottieAnimationView
              android:id="@+id/animationView"
              android:layout_width="match_parent"
              android:layout_height="300dp"
              android:layout_alignParentTop="true"
              android:layout_centerHorizontal="true"
              android:scaleType="fitXY"
              app:lottie_autoPlay="true"
              android:padding="10dp"
              app:lottie_loop="true"
              app:lottie_rawRes="@raw/your_json_file_here"/></RelativeLayout>
      

      ** 试试这个代码。它正在工作 **

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-08
        • 1970-01-01
        • 2020-10-24
        • 2022-10-23
        • 2023-02-17
        • 2017-08-30
        相关资源
        最近更新 更多