Search code examples
android-animationlottieandroid-motionlayout

Chage Lottie attributes for each keyframe, MotionLayout, Android Animation


How I could change Lottie and any other attributes inside MotionLayout? I have start and end layouts and I one to add middle point(KeyFrame) in which I want to change lottie_rawRes="@raw/lottie_animation(json)" or any other attribute.

My code is here:

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>

Part of my start/end layout(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" />

Solution

  • app:lottie_rawRes="@raw/splash_sun"

    CustomAttribute calls the name based on bean conventions so

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

    Would look for a method setLottie_rawRes(String str)

    And would not find it. Look in the Lottie api and find the method you want

    setAnimation(final String assetName)

    Would you use motion:attributeName="animation"

    setAnimationFromJson(String jsonString)

    would mean you use motion:attributeName="animationFromJson"

    Traditionally there is a match between attribute foo and method setFoo