How to create KeyFrameSet dynamically in Kotlin

Viewed 93

I have created a test programme for Android Kotlin Motion using XML for the design. I need to add many KeyPositions and KeyAttributes. I need to do this programmatically, not through XML. I have scoured the documentation without finding anything useful. Can anyonyone help with an example, or refer me to the documentation. (Programmatically in Kotlin, not XML)

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/motion_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/activity_main_scene"
    tools:context=".MainActivity">
    <ImageView
        android:id="@+id/btn_1"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:background="@drawable/btn_round"
        android:backgroundTint="#F4511E"
        android:text="Button"
        android:src="@drawable/jonathan"/>
</androidx.constraintlayout.motion.widget.MotionLayout>

activity_main_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"
    android:id="@+id/this_scene">
    <Transition
        android:id="@+id/this_trans"
        motion:constraintSetStart="@+id/start"
        motion:constraintSetEnd="@+id/end"
        motion:duration="2000"
        motion:motionInterpolator="linear"
        >
        <OnClick
            motion:targetId="@id/btn_1"
            motion:clickAction="toggle" />
        <KeyFrameSet>
            <KeyPosition
                motion:framePosition="50"
                motion:motionTarget="@id/btn_1"
                motion:keyPositionType="parentRelative"
                motion:percentX="0.8"
                motion:percentY="0.4"
                />
<!--*** Here I need many more (programmatically)-->
            <KeyAttribute
                motion:framePosition="50"
                motion:motionTarget="@id/btn_1"
                android:scaleY="2.0"
                android:scaleX="2.0"
                android:rotation="-360"
                />
<!--*** Here I need many more (programmatically)-->
        </KeyFrameSet>
    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@+id/btn_1"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="0dp"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintEnd_toEndOf="parent" />
          </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@+id/btn_1"
            android:layout_width="108dp"
            android:layout_height="108dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            />
          </ConstraintSet>
</MotionScene>
0 Answers
Related