【问题标题】:Chage Lottie attributes for each keyframe, MotionLayout, Android Animation为每个关键帧、MotionLayout、Android Animation 更改 Lottie 属性
【发布时间】:2020-11-16 11:02:43
【问题描述】:

如何更改 Lottie 和 MotionLayout 中的任何其他属性? 我有开始和结束布局,我要添加中间点(KeyFrame),我想在其中更改 lottie_rawRes="@raw/lottie_animation(json)" 或任何其他属性。

我的代码在这里:

motion_scene(xml\activity_app_splash_screen_scene.xml):

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:motion="http://schemas.android.com/apk/res-auto">

 <Transition
    motion:autoTransition="animateToEnd"
    motion:constraintSetEnd="@layout/motion_splash_screen_end"
    motion:constraintSetStart="@layout/motion_splash_screen_start"
    motion:duration="5000">
    <KeyFrameSet>

        <KeyAttribute <-------- that one
            motion:framePosition="50"
            motion:motionTarget="@+id/splashSun" />
        <KeyAttribute
            android:alpha="0"
            motion:framePosition="85"
            motion:motionTarget="@+id/splashAppName"
            />

        <KeyAttribute
            android:alpha="1"
            motion:framePosition="95"
            motion:motionTarget="@+id/splashAppName" />


    </KeyFrameSet>
 </Transition>

<ConstraintSet android:id="@layout/motion_splash_screen_end">
    <Constraint
        android:id="@+id/splashClouds1"
        >
        <CustomAttribute
            motion:attributeName="lottie_rawRes"
            motion:customStringValue="@raw/weather_moon_clear" />
    </Constraint>

</ConstraintSet>

我的开始/结束布局的一部分(layout\motion_splash_screen_start.xml):

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/splash_end"
xmlns:app="http://schemas.android.com/apk/res-auto">

<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/splashSun"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:rotation="0"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toTopOf="@+id/splashAppName"
    app:layout_constraintDimensionRatio="1.4:1"
    app:layout_constraintEnd_toStartOf="@+id/guideline2"
    app:layout_constraintStart_toStartOf="@+id/guideline1"
    app:layout_constraintTop_toTopOf="parent"
    app:lottie_autoPlay="true"
    app:lottie_loop="true"
    app:lottie_rawRes="@raw/splash_sun" />

【问题讨论】:

    标签: android-animation lottie android-motionlayout


    【解决方案1】:

    app:lottie_rawRes="@raw/splash_sun"

    CustomAttribute 根据 bean 约定调用名称 所以

    <CustomAttribute motion:attributeName="lottie_rawRes" 
     motion:customStringValue="@raw/weather_moon_clear" />
    

    会寻找方法 setLottie_rawRes(String str)

    而且找不到。 查看 Lottie api 并找到您想要的方法

    setAnimation(最终字符串资产名称)

    你会使用motion:attributeName="animation"

    setAnimationFromJson(String jsonString)

    意味着你使用 motion:attributeName="animationFromJson"

    传统上,属性 foo 和方法 setFoo 之间存在匹配

    【讨论】:

    • 谢谢,但是我怎样才能改变每一帧的属性呢?这可能吗
    • 不,不可能。在许多层面上。如果您每帧设置一个新字符串,系统将不得不以每秒 60 帧的速度解析字符串。通常,您设计一个 Lottie 动画,其中使用 setProgress 播放动画。 Lottie 不是为它设计的。
    猜你喜欢
    • 1970-01-01
    • 2021-08-02
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    相关资源
    最近更新 更多