Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Practical Animations with MotionLayout

Practical Animations with MotionLayout

MotionLayout is a new animation tool built into ConstraintLayout 2.0 that lets you easily create animations on Android. In this session, Layale will cover the basics of MotionLayout and how to get started with it using the Motion Editor in Android Studio to quickly add motion to your application!

Layale Matta

February 27, 2021
Tweet

More Decks by Layale Matta

Other Decks in Programming

Transcript

  1. We can use animations to: • Show changes • Draw

    attention • Build beautiful designs TECH(K)NOW DAY
  2. MotionLayout • Subclass of ConstraintLayout • Easily coordinate animations of

    multiple views • Complex animations driven by interactions • Seekable and reversible animations • Compatible back to API 14 TECH(K)NOW DAY
  3. Motion Editor • New animation tool • Built into Android

    Studio 4.0 • Helps build, edit and preview animations TECH(K)NOW DAY
  4. <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/iv_droid" android:layout_width="180dp" android:layout_height="180dp"

    android:src="@drawable/ic_droid" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
  5. <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/iv_droid" android:layout_width="180dp" android:layout_height="180dp"

    android:src="@drawable/ic_droid" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
  6. <androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" app:layoutDescription="@xml/activity_main_scene"> <ImageView android:id="@+id/iv_droid" android:layout_width="180dp" android:layout_height="180dp"

    android:src="@drawable/ic_droid" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.motion.widget.MotionLayout>
  7. <androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" app:layoutDescription="@xml/activity_main_scene"> <ImageView android:id="@+id/iv_droid" android:layout_width="180dp" android:layout_height="180dp"

    android:src="@drawable/ic_droid" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.motion.widget.MotionLayout>
  8. Motion Scene • XML file • Contains multiple states and

    transitions between them • Defines all animations for the scene • Can be defined programmatically TECH(K)NOW DAY
  9. ConstraintSet / State ... <ConstraintSet android:id="@+id/start"> </ConstraintSet> <ConstraintSet android:id="@+id/end"> <Constraint

    android:id="@+id/iv_droid" android:layout_width="180dp" android:layout_height="180dp" motion:layout_constraintRight_toRightOf="parent" motion:layout_constraintTop_toTopOf="parent" /> </ConstraintSet> ...
  10. ConstraintSet / State ... <ConstraintSet android:id="@+id/end"> <Constraint android:id="@+id/iv_droid" android:layout_width="180dp" android:layout_height="180dp"

    motion:layout_constraintRight_toRightOf="parent" motion:layout_constraintTop_toTopOf="parent"> <Transform android:rotation="45" android:translationZ="20dp" android:rotationY="10"/> </Constraint> </ConstraintSet> ...
  11. Transition ... <Transition motion:constraintSetEnd="@+id/end" motion:constraintSetStart="@id/start" motion:duration="1000"/> <ConstraintSet android:id="@+id/start"> </ConstraintSet> <ConstraintSet

    android:id="@+id/end"> <Constraint android:id="@+id/iv_droid" android:layout_width="180dp" android:layout_height="180dp" motion:layout_constraintRight_toRightOf="parent" motion:layout_constraintTop_toTopOf="parent" /> </ConstraintSet> ...
  12. Transition Attributes TECH(K)NOW DAY android:id Name your transition transitionDisable Disable/Enable

    your transition pathMotionArc All layout movement in arcs autoTransition Automatically transition motionInterpolator Configure easing duration Time to go from start to end in ms staggered Sequentially execute individual transitions