Share
Embed
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
Transcript
39 slides
-
We can use animations to: • Show changes • Draw
attention • Build beautiful designs TECH(K)NOW DAY -
Get Started • MotionLayout • Motion Scene • Motion Editor
• Demo TECH(K)NOW DAY -
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 -
Motion Editor • New animation tool • Built into Android
Studio 4.0 • Helps build, edit and preview animations TECH(K)NOW DAY -
<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> -
<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> -
<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> -
<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> -
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 -
<MotionScene> <Transition> <KeyFrameSet> <OnClick> <OnSwipe> <ConstraintSet> <Constraint> <Transform> <PropertySet> <Layout>
<CustomAttribute> Components of a Motion Scene -
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> ... -
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> ... -
Transition <MotionScene xmlns:android="http://schemas.android.com/apk/res/android" xmlns:motion="http://schemas.android.com/apk/res-auto"> <Transition motion:constraintSetEnd="@+id/end" motion:constraintSetStart="@id/start" motion:duration="1000"/> <ConstraintSet android:id="@+id/start">
</ConstraintSet> <ConstraintSet android:id="@+id/end"> </ConstraintSet> </MotionScene> -
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> ... -
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 -
OnClick <MotionScene xmlns:android="http://schemas.android.com/apk/res/android" xmlns:motion="http://schemas.android.com/apk/res-auto"> <Transition motion:constraintSetEnd="@+id/end" motion:constraintSetStart="@id/start" motion:duration="1000"> <OnClick motion:targetId="@+id/iv_droid"
motion:clickAction="toggle"/> </Transition> ... </MotionScene> -
Learn More TECH(K)NOW DAY Documentation d.android.com/training/constraint-layout/motionlayout Codelab d.android.com/codelabs/motion-layout GitHub Samples
github.com/android/views-widgets-samples/tree/master/ConstraintLayoutExamples