Lottie on Android: Part 3 — Dynamic properties

Connie Reinholdsson
Jul 2, 2019 · 4 min read
Image for post
Image for post

This series of articles follows as a result of mine and Matt Carron’s talk ‘Three approaches to animations on Android’ at the CodeMobile Developer Conference 2019, where we evaluate three approaches to animations including the Android framework, Video and Airbnb’s Lottie library.

Image for post

The Github repository with all the examples from this series can be found here. The Lottie animation used in this tutorial is by LottieFiles and can be found here.

Dynamic Properties

So what are dynamic properties? Dynamic properties allows us to target specific animation properties and update them at runtime in response to an event, for example a button click, a success or error state or a download completed event. This sounds pretty cool, but how does it actually work?

In After Effects (AE) where the animation is built, it has a hierarchy of animation properties which is called the animation’s composition. The composition is in turn made of up of a collection of layers. So what’s a layer? A layer is an object with a string name and has similar attributes to a drawable, for example fills and strokes. These layers make up the animation’s hierarchy. So by targeting layers in our code, we can amend the properties of the animation. Confused? Don’t worry, we’ll show lots of examples.

But first, how can we find out what the animation hierarchy looks like?

Discover the KeyPath

By running the resolveKeyPath() method on the animation view we target all the layers using the a Globstar “**” (more info below) and print each layer in the logcat.

And below is the result! ⬇️

This is not the whole composition, it goes on and on… But what we can see here is how each animation layer makes up the data structure of the animation. Time to start updating them at runtime!

Image for post
Image for post

1. Target one specific layer

The first use case is to target one specific layer and change its colour to green in response to a button click. In order to do so, we need to target the full path. In this case, we’re targeting the Crown Outlines, in Group 1 and changing Fill 1 to green.

And here’s the result, pretty simple right? 😄

Image for post
Image for post

2. Use Wildcards (“*”)

The second use case is to use a Wildcard “*” which allows us to target several layers that ends in the same attributes. By passing in one star to the KeyPath“*” , that’s saying `let’s make all layers ending in the Group 1, Fill 1 be green`. Let’s try it!

And here’s the result! So as we can see, layers that end in the same attributes are updated. 👍

Image for post
Image for post

3. Use Globstars (“**”)

Finally, we will use a Globstar “**” which allows us to target all layers ending in the same attribute (this is what we used to log all the layers earlier to print the hierarchy in the logcat). By passing in two stars to KeyPath “**” that’s saying `let’s make all layers ending in the one attribute Fill 1 to be green`. Let’s try it!

And here’s the result:

Image for post
Image for post

Pretty cool, huh?

So that’s it for dynamic properties. This is a short demonstration of using them, but there are lots of properties that can be amended including color, opacity, stroke width, stroke colour and an endless number of use cases… It would be great to see what other people have done with dynamic properties!

Thanks for reading and best of luck with your animations! 😄

Compare the Market

The people behind comparethemarket.com and the Meerkat App

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