Animation: Jump-through

Nick Butcher
Dec 5, 2016 · 5 min read

Recently a call for help caught my eye; asking how to implement a fancy ‘getting location’ animation on Android:

Image for post
Image for post by @lekarew

I immediately thought this was a prime candidate for an AnimatedVectorDrawable (AVD hereafter) so set out to demonstrate one way to implement this. Some people asked how I did this… so here’s a breakdown.

For an introduction to AnimatedVectorDrawable’s capabilities, I highly recommend this post by Alex Lockwood

Looking at the composition, this is made up of three types of animation:

  1. The pin moves & changes shape to make it jump; AVD supports this by animating the actual shape of a path, known as path morphing.
  2. The dots move leftward, this will be a simple translate.
  3. The dots fade in/out as the enter/exit the scene.

Unfortunately I didn’t have access to the source artwork, just the GIF on dribbble… hopefully you won’t have to do this in your animations!

I opened the GIF in Photoshop which provides a timeline view of the frames of the animation. I stepped through this and saved copies of frames where the pin was at extremes of movement i.e. about to change direction or change shape significantly. These ‘poses’ will be the shapes that the pin will morph between. In total there were 5 major poses.

I pasted each of these into Sketch (my preferred vector drawing tool) and traced out the pin in each of the poses.

Image for post
Image for post
The 5 shapes the pin ‘morphs’ between
Image for post
Image for post

Some tools can automatically trace raster images and produce vector paths for you. Knowing that I wanted to perform a path morph animation however, meant that I needed ‘compatible paths’; that is each path needs to have the same number and type of points. As such by drawing the pin with a simple shape (made up of 8 points, each with 2 control points) and manually altering it to form each pose I could be sure that we can later animate between the shapes.

I then exported each frame as individual SVG files. Now SVG is a vast spec and Android’s VectorDrawable only supports a subset of it. It’s also helpful to remember that they need to be parsed and rendered on a constrained mobile device; therefore I always try to simplify my SVGs. To do this, I ran them through SVGO (using SVGOMG by Jake Archibald) setting the precision to 1 decimal place.

Now that we have the paths of each pose of the pin, it’s time to make them move! To do this I turned to the awesome Icon Animator by Roman Nurik. This is a web-based IDE for creating AnimatedVectorDrawables. I dropped in the first SVG and then clicked on the stopwatch icon next to the pin layer and added a new pathData animation. This produces an inspector panel where I could insert the path of the pin from the next frame into the toValue. I repeated this 4 more times to make our animation (where the last step animates from the final pose, back to the initial one).

For the timing values I referred back to the original GIF where each frame lasted 30ms so if there was 4 frames between poses, use a duration of 120ms etc. I chose to just use standard material interpolators, but in reality this could benefit from fine tuning.

For the dots which move along we can perform a simple translation. Looking at the source composition, every third dot is larger and red; how to implement this in a nice loop? While you could implement this with just 3 dots and animate each individually; I decided to include 5 dots where the rightmost dots extend beyond the viewport:

Image for post
Image for post

Doing this allows us to animate the entire group of dots leftward all together, once the 4th dot reaches the initial location of the 1st dot, we instantaneously reset the translation, jumping it back to it’s starting point. Neat!

We want the dots to fade in as they enter and fade out as they leave, this is a simple animation of the fillAlpha, I leave that up to you to see how it’s achieved!

Now to make this into a complete looping animation there are a few things to consider. The dot animation has triple the duration of the pin jumping. I considered breaking them into 2 separate AVDs and layering them… but decided that repeating the jump 3 times was simpler!

I hit the magical export button in Icon Animator which gave me the AVD definition, ready to drop into my project. The tool however is still in ‘preview’ so I made a few tweaks to it to clean up the output, namely removing any redundant attributes and factoring out any repeated values (like the different pin paths and some colors) to a resources file to make them more maintainable.

Actually making the full AVD loop was trickier than i’d envisaged. Apparently repeatMode on animations sets are ignored 😡 (bugs have been filed). I worked around this by listening for the end of the animation and starting it again. Unfortunately callbacks were only added in API23 but you could achieve the same effect via postDelayed etc.

You can find a gist of the code here or check out the full composition in Icon Animator and have a play with it.

Hopefully this post has shown you that AnimatedVectorDrawable is very powerful and that you can create rich animations relatively easily. I also hope that sharing my workflow helps to demystify the process and maybe inspires you to create your own. Let me know if you do!

Android Developers

The official Android Developers publication on Medium

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store