0

I am creating animation in splashScreen with motionLayout. But my animation doesn't work, instead it immediatly shows the ending positions of the elements on the screen. Here is my code:

SplashActivity:

class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_splash)

    motionLayout.transitionToEnd()

    val handler = Handler()
    handler.postDelayed( {
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }, 3000)
}
}

activity_splash layout :

    <?xml version="1.0" encoding="utf-8"?>
   <androidx.constraintlayout.motion.widget.MotionLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:background="@color/colorPrimary"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:id="@+id/motionLayout"
   app:layoutDescription="@xml/scene_splash"
   tools:context=".ui.main.MainActivity">

<TextView
    android:id="@+id/letter1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="S"
    android:textColor="#000"
    android:textSize="40sp" />


<TextView
    android:id="@+id/letter2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="T"
    android:textColor="#000"
    android:textSize="40sp" />

<TextView
    android:id="@+id/letter3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="O"
    android:textColor="#000"
    android:textSize="40sp"
    android:fontFamily="sans-serif-black" />

<TextView
    android:id="@+id/letter4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="R"
    android:textColor="#000"
    android:textSize="40sp" />

<TextView
    android:id="@+id/letter5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="I"
    android:textColor="#000"
    android:textSize="40sp" />

<TextView
    android:id="@+id/letter6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="E"
    android:textColor="#000"
    android:textSize="40sp" />

<TextView
    android:id="@+id/letter7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="S"
    android:textColor="#000"
    android:textSize="40sp"
    android:fontFamily="sans-serif-black" />

<TextView
    android:id="@+id/word"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="by UNRD"
    android:textColor="#000"
    android:textSize="40sp"
    android:fontFamily="sans-serif-black"/>

</androidx.constraintlayout.motion.widget.MotionLayout>

and finally my scene_splash file:

<?xml version="1.0" encoding="utf-8"?>

<Transition
    motion:constraintSetStart="@+id/start"
    motion:constraintSetEnd="@+id/end"
    motion:duration="2500">

    <KeyFrameSet>
        <KeyPosition
            motion:keyPositionType="parentRelative"
            motion:percentY="0.25"
            motion:framePosition="50"
            motion:motionTarget="@id/letter2"/>

        <KeyPosition
            motion:keyPositionType="parentRelative"
            motion:percentY="0.40"
            motion:percentX="0.1"
            motion:framePosition="50"
            motion:motionTarget="@id/letter3"/>

        <KeyPosition
            motion:keyPositionType="parentRelative"
            motion:percentY="0.70"
            motion:framePosition="50"
            motion:motionTarget="@id/letter6"/>
    </KeyFrameSet>
</Transition>

<ConstraintSet android:id="@+id/start">
    <Constraint
        android:id="@+id/letter1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintRight_toRightOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>

    <Constraint
        android:id="@+id/letter2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintBottom_toBottomOf="parent"
        />

    <Constraint
        android:id="@+id/letter3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintTop_toTopOf="parent"
        motion:layout_constraintRight_toRightOf="parent"
        motion:layout_constraintVertical_bias="0.8"
        motion:layout_constraintBottom_toBottomOf="parent"/>

    <Constraint
        android:id="@+id/letter4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintVertical_bias="0.15"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintRight_toRightOf="parent"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>

    <Constraint
        android:id="@+id/letter5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="20dp"
        motion:layout_constraintBottom_toBottomOf="parent"/>

    <Constraint
        android:id="@+id/letter6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="30dp"/>

    <Constraint
        android:id="@+id/letter7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintRight_toRightOf="parent"
        android:layout_marginBottom="30dp"
        motion:layout_constraintBottom_toBottomOf="parent"/>

    <Constraint
        android:id="@+id/word"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:alpha="0"
        android:layout_marginTop="100dp"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintRight_toRightOf="parent"
        motion:layout_constraintTop_toBottomOf="parent"
        android:fontFamily="sans-serif-black"/>

</ConstraintSet>

<ConstraintSet android:id="@+id/end">

    <Constraint
        android:id="@+id/letter1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintVertical_bias="0.45"
        motion:layout_constraintRight_toLeftOf="@id/letter2"
        motion:layout_constraintTop_toTopOf="parent"/>


    <Constraint
        android:id="@+id/letter2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintVertical_bias="0.45"
        motion:layout_constraintRight_toLeftOf="@id/letter3"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>

    <Constraint
        android:id="@+id/letter3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintVertical_bias="0.45"
        motion:layout_constraintRight_toLeftOf="@id/letter4"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>

    <Constraint
        android:id="@+id/letter4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintVertical_bias="0.45"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintRight_toRightOf="parent"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>

    <Constraint
        android:id="@+id/letter5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintVertical_bias="0.45"
        motion:layout_constraintLeft_toRightOf="@id/letter4"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>

    <Constraint
        android:id="@+id/letter6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintVertical_bias="0.45"
        motion:layout_constraintLeft_toRightOf="@id/letter5"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>

    <Constraint
        android:id="@+id/letter7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintVertical_bias="0.45"
        motion:layout_constraintLeft_toRightOf="@id/letter6"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>

    <Constraint
        android:id="@+id/word"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rotation="20"
        android:layout_marginTop="20dp"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintRight_toRightOf="parent"
        motion:layout_constraintTop_toBottomOf="@id/letter4"/>
</ConstraintSet>

Does anyone has an idea why is this happening?

Kratos
  • 681
  • 1
  • 13
  • 30

1 Answers1

3

You can simply add in <Transition

motion:autoTransition="animateToEnd"

Then no need to call transitionToEnd.

Alternatively You can call transtionToEnd

     onAttachedToWindow() {
        super.onAttachedToWindow()
        motionlayout.transitionToEnd()
hoford
  • 4,918
  • 2
  • 19
  • 19
  • neither of this doesn't help. My animation stil doesn't happen... – Kratos Mar 25 '21 at 11:13
  • Pardon me. motion:autoTransition="animateToEnd" solves the problem. Thank you very much, I will upvote and accept your answer. If you have any idea why my approach doesn't work, but this way by adding in xml it works please explain me, because I don't have any logical explanation. – Kratos Mar 25 '21 at 13:09
  • The scene is only setup in onAttachedToWindow so transitionToEnd is meaningless in on create. – hoford Apr 04 '21 at 00:04