by Shaun Lawrence
Lottie is one of the best discoveries that I have made in this past year. It has been around much longer, but it took a friend and a side project to help me discover it.
Lottie is an animation library that was built and open sourced by Airbnb. It is a mobile library for Android and iOS that parses Adobe After Effects animations exported to JSON files with Bodymovin and renders them natively on mobile!
Don’t worry neither am I! Don’t get me wrong I think I have these great ideas, but they never translate well on to paper or whatever design tool I am using. We don’t need to worry though because there are great designers out there that build animations and share them on Lottie Files. This site curates a load of free and paid for animations that we can download and embed in our applications.
For our mobile game we are going to make use of the following pre-built animation from Lottie Files:
You will notice that the background animation does not match our colour scheme in the app. Thankfully Lottie Files also provides an editor that lets you change the colours and other elements. I have gone ahead and already edited this in Lottie Files. You can do the same or feel free to grab it from the repo here.
Now that we have the files lets jump over to Visual Studio and get them running in our app.
We need to add the files to both our Android and iOS projects.
The steps you need to follow are:
Pairs.Android
background.json
file and add it.The steps you need to follow are:
Pairs.iOS
background.json
file and add it.Next we need to go ahead and grab the NuGet package ready to show off our new fancy animation.
Pairs
solutionCom.Airbnb.Xamarin.Forms.Lottie
You should notice that a ReadMe.txt file is opened automatically, this gives a helpful way of seeing the possibilities that come with the package. We certainly won’t need to use it all at least for this scenario but I thoroughly recommend checking out the repository here https://github.com/Baseflow/LottieXamarin
Let’s jump over to our MainPage.xaml
file and add some code changes.
First add the lottie namespace at the top:
clr-namespace:Lottie.Forms;assembly=Lottie.Forms
Next let’s add an AnimationView
to render the background.json
file. We want to place this inside our <Grid>
as the first item. We add it as the first item to make sure it renders behind any other items.
<lottie:AnimationView Animation="background.json"
Grid.RowSpan="2"
Opacity="0.5"
AnimationSource="AssetOrBundle"
RepeatMode="Infinite" />
Hopefully most of the changes are self explanatory but here are some key details:
Animation="background.json"
- this points to the file we included in our projects.AnimationSource="AssetOrBundle"
- this relates to how we included the file in our projects. Remember the Build Action we set when adding the files.RepeatMode="Infinite"
- this will cause the animation to continually play.Now let’s run the app and see the result.
I hope this shows how we can add some really cool animations to our applications so that it provides the feeling that things are alive.
The source for the end of this stage can be found at:
https://github.com/bijington/mobile-game-xamarin-forms/tree/part-seven-lottie
Previous | Next |
---|---|
Shapes/Paths + Converters | Behaviors - COMING SOON |