【问题标题】:Lottie Animation width not setting in android matchparentLottie 动画宽度未在 android match_parent 中设置
【发布时间】:2019-06-05 05:07:28
【问题描述】:

你好,我正在使用这个 Lottie 动画https://lottiefiles.com/117-progress-bar。当我说匹配父母时,这就是我所看到的。我需要增加宽度。我怎样才能做到这一点?

  <LinearLayout
        android:layout_weight="0.5"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:background="@android:color/holo_red_dark"
        android:orientation="vertical">


    <com.airbnb.lottie.LottieAnimationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:lottie_autoPlay="true"
        app:lottie_loop="true"
        app:lottie_fileName="progress_bar.json"
        android:visibility="visible"/>

    <TextView
        android:id="@+id/transmitting"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:background="@android:color/white"
        android:fontFamily="@font/open_sans_semibold"
        android:text="Transmitting..."
        android:textAlignment="center"
        android:textSize="15dp"
        android:visibility="invisible"
        />

    </LinearLayout>

【问题讨论】:

    标签: android width lottie


    【解决方案1】:

    有时,创建的 Lottie 动画不会填满整个视口。您必须在dp 中手动提供layout_widthlayout_height,或根据需要缩放drawable。您无法更改提供的 .json 文件中的尺寸,因为所有属性值都相对于原始尺寸。

    您可以尝试下面的代码来缩放LottieAnimationView内的Drawable

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

    如果您使用上述代码动态添加动画,请不要忘记从您的LottieAnimationView 中删除app:lottie_fileName 标签。

    【讨论】:

    • 你在设置比例时从哪里得到4
    • 这是您要设置的比例。你可以随意调整它。根据提供的图像,我认为 4 是给定 Lottie Animation 的合适比例。
    • 所以你只是根据估计来衡量?
    • 是的,没有任何其他方法可以在不创建具有更新属性值的新对象的情况下缩放 LottieAnimationView 中的可绘制对象。有些动画无需缩放即可工作,它们在创建动画时可以适当地放在画布上。
    猜你喜欢
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    相关资源
    最近更新 更多