MotionLayout builds transitions
This article will show you how to quickly create a simple transition effect using MotionLayout.
There are only three steps to building the transition
Before you begin, keep in mind that building transitions is a three-step process.
- Create the StartScene
- Create an EndScene
- Adjust the way the scene starts and ends
Let’s cut to the chase
Build the start scene
First, we have a starting scene
<ConstraintSet android:id="@+id/start">
<Constraint android:layout_width="wrap_content"
android:id="@id/guide"
android:orientation="horizontal"
motion:layout_constraintGuide_percent="0.5"
android:layout_height="wrap_content"/>
<Constraint android:id="@id/h"
android:layout_width="30dp"
android:layout_height="40dp"
motion:layout_constraintTop_toTopOf="parent"
motion:layout_constraintStart_toStartOf="parent"/>
<! --E ... R code omitted -->
</ConstraintSet>
Copy the code
Build the End scene
Then, we have an ending scene
<ConstraintSet android:id="@id/end">
<Constraint android:layout_width="wrap_content"
android:id="@id/guide"
android:orientation="horizontal"
motion:layout_constraintGuide_percent="0.5"
android:layout_height="wrap_content"/>
<Constraint android:layout_width="30dp"
android:layout_height="40dp"
motion:layout_constraintHorizontal_chainStyle="packed"
motion:layout_constraintTop_toTopOf="@id/guide"
motion:layout_constraintBottom_toBottomOf="@id/guide"
motion:layout_constraintEnd_toStartOf="@id/e1"
android:id="@id/h"
motion:layout_constraintStart_toStartOf="parent"/>
<! --E ... R code omitted -->
</ConstraintSet>
Copy the code
Build the transition
Finally, adjust how the start scene ends
<Transition motion:constraintSetStart="@+id/start"
motion:constraintSetEnd="@+id/end"
motion:duration="1200">
<! -- H slide to change transition state -->
<OnSwipe motion:dragDirection="dragRight"
motion:touchAnchorSide="right"
motion:touchAnchorId="@id/h"/>
<KeyFrameSet>
<! -- Position keyframes using path coordinates -->
<KeyPosition motion:target="@id/h"
motion:framePosition="85"
motion:percentX="0.8"
motion:percentY="0.15"
motion:keyPositionType="pathRelative"/>
<KeyPosition motion:target="@id/e1"
motion:framePosition="85"
motion:percentX="0.9"
motion:percentY="0.25"
motion:keyPositionType="pathRelative"/>
<KeyPosition motion:target="@id/n"
motion:framePosition="40"
motion:percentX="0.7"
motion:percentY="0.25"
motion:keyPositionType="pathRelative"/>
<! -- Set two keyframes for C -->
<KeyPosition motion:target="@id/c"
motion:framePosition="85"
motion:percentX="0.95"
motion:percentY="0.25"
motion:keyPositionType="pathRelative"/>
<KeyPosition motion:target="@id/c"
motion:framePosition="35"
motion:percentX="0.35"
motion:percentY="0.05"
motion:keyPositionType="pathRelative"/>
<KeyPosition motion:target="@id/o"
motion:framePosition="35"
motion:percentX="0.05"
motion:percentY="0.2"
motion:keyPositionType="pathRelative"/>
<KeyPosition motion:target="@id/o"
motion:framePosition="85"
motion:percentX="0.85"
motion:percentY="0.2"
motion:keyPositionType="pathRelative"/>
<KeyPosition motion:target="@id/d"
motion:framePosition="15"
motion:transitionEasing="accelerate"
motion:keyPositionType="deltaRelative"
motion:percentY="0.15"/>
<KeyPosition motion:target="@id/d"
motion:framePosition="85"
motion:transitionEasing="decelerate"
motion:percentX="1.1"
motion:percentY="0.45"
motion:keyPositionType="pathRelative"/>
<! -- Finish 90% of the time -->
<KeyPosition motion:target="@id/e2"
motion:framePosition="90"
motion:percentX="1"
motion:percentY="0"
motion:keyPositionType="pathRelative"/>
<! -- Differential coordinate system -->
<KeyPosition motion:target="@id/e2"
motion:framePosition="95"
motion:percentX="1"
motion:percentY="1.2"
motion:keyPositionType="deltaRelative"/>
</KeyFrameSet>
</Transition>
Copy the code
So we have the whole transition
Trigger a transition
Of course, you can also add events to the target to initiate the transition
<Transition motion:constraintSetStart="@+id/start"
motion:constraintSetEnd="@+id/end"
motion:duration="1200">
<! -- R Toggle start/end state -->
<OnClick motion:clickAction="toggle"
motion:target="@id/r"/>
<! -- Left E transition to initial state -->
<OnClick motion:clickAction="transitionToStart"
motion:target="@id/e1"/>
<! -- Right E transition to end state -->
<OnClick motion:clickAction="transitionToEnd"
motion:target="@id/e2"/>
<KeyFrameSet>
<! -- -- -- > slightly
</KeyFrameSet>
</Transition>
Copy the code
The source code
Go to Github to see the source code