Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Hi Android Developer @ Over Google Developer Expert in Android @riggaroo riggaroo.co.za I’m Rebecca Franks "

Slide 4

Slide 4 text

JellyToolbar https://github.com/Yalantis/JellyToolbar Alexio Mota & Nick Butcher https://twitter.com/alexiomota TiVi - Chris Banes https://github.com/chrisbanes/tivi Examples of Animations Android Dev Summit 2018 App

Slide 5

Slide 5 text

But how?

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

All Kinds of Animations ✨ • AnimatedVectorDrawables • Shape Shifter • Property Animations • Custom View Animations • Physics-based Animations • MotionLayout

Slide 8

Slide 8 text

Sample App - Github bit.ly/animation-ex

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

AnimatedVectorDrawable • VectorDrawable and AnimatorSet / ObjectAnimator • VectorDrawable: Scalable, density-independent asset • Represented by paths • Subset of SVG spec • Fire and forget animation

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Shape Shifter • Create Animations for Android, iOS and Web • Animate pathData, alpha, color, strokeWidth • Made by Alex Lockwood

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Step 1 : Choose/Make your icons Step 2 : Import them into Shape Shifter Step 3 : Animate pathData between them Step 4 : Magic?

Slide 16

Slide 16 text

Step 1 : Choose your icons Might need to split the icons up logically in Sketch

Slide 17

Slide 17 text

Step 2 : Import into Shape Shifter

Slide 18

Slide 18 text

Step 3 : Animate pathData

Slide 19

Slide 19 text

(Maybe) Step 4 : Pair sub paths

Slide 20

Slide 20 text

(Maybe) Step 5 : Correct points

Slide 21

Slide 21 text

Final Animation

Slide 22

Slide 22 text

AnimatedVectorDrawable • Export from Shape Shifter as AVD file

Slide 23

Slide 23 text

val drawable = AppCompatResources.getDrawable(this, R.drawable.avd_center_justify) imageViewAvd.setImageDrawable(drawable) AnimatedVectorDrawableCompat.registerAnimationCallback(drawable, object : Animatable2Compat.AnimationCallback() { override fun onAnimationEnd(drawable: Drawable?) { Toast.makeText(applicationContext, "Animation finished", Toast.LENGTH_LONG).show() } }) imageViewAvd.setOnClickListener { (drawable as Animatable).start() }

Slide 24

Slide 24 text

val drawable = AppCompatResources.getDrawable(this, R.drawable.avd_center_justify) imageViewAvd.setImageDrawable(drawable) AnimatedVectorDrawableCompat.registerAnimationCallback(drawable, object : Animatable2Compat.AnimationCallback() { override fun onAnimationEnd(drawable: Drawable?) { Toast.makeText(applicationContext, "Animation finished", Toast.LENGTH_LONG).show() } }) imageViewAvd.setOnClickListener { (drawable as Animatable).start() }

Slide 25

Slide 25 text

val drawable = AppCompatResources.getDrawable(this, R.drawable.avd_center_justify) imageViewAvd.setImageDrawable(drawable) AnimatedVectorDrawableCompat.registerAnimationCallback(drawable, object : Animatable2Compat.AnimationCallback() { override fun onAnimationEnd(drawable: Drawable?) { Toast.makeText(applicationContext, "Animation finished", Toast.LENGTH_LONG).show() } }) imageViewAvd.setOnClickListener { (drawable as Animatable).start() }

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Lottie • Useful for Cross platform complex Animations • Created by Airbnb • Use AfterEffects & export using Bodymovin’ • setProgress() • Download on demand • lottiefiles.com

Slide 29

Slide 29 text

Lottie

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

ViewPropertyAnimator • Animate specific, common properties on a view • Fire and Forget • User-friendly API to use • More efficient when doing multiple animations at once

Slide 32

Slide 32 text

Property Animations imageView .animate() .alpha(0f) .rotationBy(360f) .scaleX(0f) .scaleY(0f) .setDuration(2000) .withEndAction { // Animation Finished } .start()

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Custom View Animations • Built in animations just don’t cut it • Custom properties on a view • Color, radius, shadow, matrix

Slide 36

Slide 36 text

PropertyValuesHolder • Holds info about a property & values during animation • Create animations with ValueAnimator or ObjectAnimator

Slide 37

Slide 37 text

fun animateSizeColorChange(toSize: Float, @ColorInt toColor: Int) { val propColor = PropertyValuesHolder.ofInt(PROP_TEXT_COLOR, textColor, toColor) val propTextSize = PropertyValuesHolder.ofFloat(PROP_TEXT_SIZE, size, toSize) ValueAnimator.ofPropertyValuesHolder(propColor, propertyTextSize).apply { setEvaluator(ArgbEvaluator()) duration = 500 addUpdateListener { animation -> val textSize = animation.getAnimatedValue(PROP_TEXT_SIZE) as Float val textColor = animation.getAnimatedValue(PROP_TEXT_COLOR) as Int setTextSizeColor(textSize, textColor) } start()

Slide 38

Slide 38 text

fun animateSizeColorChange(toSize: Float, @ColorInt toColor: Int) { val propColor = PropertyValuesHolder.ofInt(PROP_TEXT_COLOR, textColor, toColor) val propTextSize = PropertyValuesHolder.ofFloat(PROP_TEXT_SIZE, size, toSize) ValueAnimator.ofPropertyValuesHolder(propColor, propertyTextSize).apply { setEvaluator(ArgbEvaluator()) duration = 500 addUpdateListener { animation -> val textSize = animation.getAnimatedValue(PROP_TEXT_SIZE) as Float val textColor = animation.getAnimatedValue(PROP_TEXT_COLOR) as Int setTextSizeColor(textSize, textColor) } start() } }

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Physics-Based Animations / Dynamic Animations • Driven by force - ie gestures • More natural looking • Don’t specify duration • Can reconfigure and velocity is recalculated • SpringAnimation & FlingAnimation

Slide 43

Slide 43 text

Fling Animation FlingAnimation(imageView, DynamicAnimation.TRANSLATION_Y) .apply { setStartVelocity(700f) // Pixels per second friction = 0.1f }.start() https://developer.android.com/guide/topics/graphics/fling-animation

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

Spring Animation

Slide 46

Slide 46 text

Spring Animation private val floatPropertyAnimX = object : FloatPropertyCompat(PROPERTY_X) { override fun setValue(dropper: ColorView?, value: Float) { dropper?.setDropperX(value) } override fun getValue(dropper: ColorView?): Float { return dropper?.getDropperX() ?: 0f } }

Slide 47

Slide 47 text

Spring Animation SpringAnimation(this, floatPropertyAnimX, point.x).apply { spring.stiffness = SpringForce.STIFFNESS_MEDIUM spring.dampingRatio = SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY start() }

Slide 48

Slide 48 text

Spring Animation - KTX ✨ springAnimationOf(::setDropperX, ::getDropperX, point.x).apply { spring.stiffness = SpringForce.STIFFNESS_MEDIUM spring.dampingRatio = SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY start() }

Slide 49

Slide 49 text

STIFFNESS_LOW STIFFNESS_MEDIUM STIFFNESS_HIGH

Slide 50

Slide 50 text

DAMPING_RATIO_LOW_BOUNCY DAMPING_RATIO_MEDIUM_BOUNCY DAMPING_RATIO_HIGH_BOUNCY

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

MotionLayout • Subclass of ConstraintLayout • Complex UI element animations • Gesture driven UI changes • Useful for transitions between layout states • Defined in XML

Slide 53

Slide 53 text

MotionLayout - Basic Example • dragUp on RecyclerView

Slide 54

Slide 54 text

Slide 55

Slide 55 text

Slide 56

Slide 56 text

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

MotionLayout - KeyFrames • Specify certain animations at certain time within complex animation Start state End state framePosition = 0 100 60 View is fully visible

Slide 59

Slide 59 text

[…]

Slide 60

Slide 60 text

subscriptionMotionLayout.transitionToEnd()

Slide 61

Slide 61 text

KeyFrames

Slide 62

Slide 62 text

Examples • bit.ly/animation-ex • bit.ly/cl-samples

Slide 63

Slide 63 text

Recap • AVDs • Shape Shifter • Lottie • Property Animations • Custom View Animations • MotionLayout

Slide 64

Slide 64 text

There are other ways… • Transitions • TransitionManager • Activity Scenes • AnimatorSets • ….

Slide 65

Slide 65 text

Spend time learning the available tools, so you can make some great animations ✨

Slide 66

Slide 66 text

@riggaroo

Slide 67

Slide 67 text

Resources • https://proandroiddev.com/android-bring-life-to-your-custom- view-8604ab3967b3 • https://android-developers.googleblog.com/2011/05/ introducing-viewpropertyanimator.html • https://twitter.com/chrisbanes/status/1030365248694312960

Slide 68

Slide 68 text

Resources • https://youtu.be/N_x7SV3I3P0 • https://medium.com/s23nyc-tech/geometric-android- animations-using-the-canvas-dd687c43f3f4