Android Animation Tutorial with Kotlin
In this Android animation tutorial, you will learn how to use property animations to make a fun, beautiful user interface. By Kevin D Moore.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Android Animation Tutorial with Kotlin
35 mins
- Getting Started
- How do Property Animations Work?
- Time Interpolators
- Your First Animation
- Launch the Rocket
- Put a Spin on It
- Accelerate the Launch
- Which Properties Can You Animate?
- ObjectAnimator
- Animating Color
- Combining Animations
- ViewPropertyAnimator
- Animating the Same Property of Two Objects
- Animation Listeners
- Animation Options
- Declaring Animations in XML
- Where To Go From Here?
Launch the Rocket
Doge isn’t going anywhere unless you initiate the rocket launch, and it’s the best animation to start with because it’s pretty easy. Who’d have thought that rocket science is so simple?
Open LaunchRocketValueAnimatorAnimationActivity.kt, and add the following code to the body of onStartAnimation()
:
//1
val valueAnimator = ValueAnimator.ofFloat(0f, -screenHeight)
//2
valueAnimator.addUpdateListener {
// 3
val value = it.animatedValue as Float
// 4
rocket.translationY = value
}
//5
valueAnimator.interpolator = LinearInterpolator()
valueAnimator.duration = DEFAULT_ANIMATION_DURATION
//6
valueAnimator.start()
- Create an instance of
ValueAnimator
by calling the static methodofFloat
. It accepts the floating point numbers that’ll apply to the specified property of the animated object over time. In this case, the values start at0f
and end with-screenHeight
. Android starts screen coordinates at the top-left corner, so the rocket’s Y translation changes from 0 to the negative of the screen height — it moves bottom to top. - Call
addUpdateListener()
and pass in a listener.ValueAnimator
calls this listener with every update to the animated value — remember the default delay of 10 ms. - Get the current value from the animator and cast it to a float; the current value type is
float
because you created theValueAnimator
withofFloat
. - Change the rocket’s position by setting its
translationY
value. - Set up the animator’s duration and interpolator.
- Start the animation.
Build and run. Select Launch a Rocket in the list. You’ll get a new screen. Tap it!
That was fun, right? :] Don’t worry about Doge getting left behind — he’ll catch his rocketship to the moon a bit later.
Put a Spin on It
How about giving the rocket a little spin action? Open RotateRocketAnimationActivity.kt and add the following to onStartAnimation()
:
// 1
val valueAnimator = ValueAnimator.ofFloat(0f, 360f)
valueAnimator.addUpdateListener {
val value = it.animatedValue as Float
// 2
rocket.rotation = value
}
valueAnimator.interpolator = LinearInterpolator()
valueAnimator.duration = DEFAULT_ANIMATION_DURATION
valueAnimator.start()
Can you spot the difference?
- Changing the
valueAnimator
values to go from0f
to360f
causes the rocket to make a full turn. Note that you could create a U-turn effect with0f
to180f
. - Instead of setting
translationY
, you set the rocket’srotation
because that’s what needs to change.
Build, run and select Spin a rocket. Tap on the new screen:
Accelerate the Launch
Open AccelerateRocketAnimationActivity.kt and add the following code to your old friend onStartAnimation()
:
// 1
val valueAnimator = ValueAnimator.ofFloat(0f, -screenHeight)
valueAnimator.addUpdateListener {
val value = it.animatedValue as Float
rocket.translationY = value
}
// 2 - Here set your favorite interpolator
valueAnimator.interpolator = AccelerateInterpolator(1.5f)
valueAnimator.duration = DEFAULT_ANIMATION_DURATION
// 3
valueAnimator.start()
The above code is identical to onStartAnimation()
in LaunchRocketValueAnimationActivity.kt except for one line: the interpolator used to set valueAnimator.interpolator
.
Build, run and select Accelerate a rocket in the list. Tap on the new screen to see how your rocket behaves. Notice how it starts slowly and then it gets accelerated.
Again, we see that poor Doge doesn’t catch the rocket to the moon…poor fella. Hang in there, buddy!
Since you used AccelerateInterpolator
, you should see your rocket accelerating after liftoff. Feel free to play around with interpolators if you’d like. I’ll sit here and wait. I promise :]
Which Properties Can You Animate?
Until now, you’ve only animated position and rotation for View
, but ValueAnimator
doesn’t care what you do with the value that it supplies.
You can tell ValueAnimator
to animate the value using any of the following types:
-
float
if you createValueAnimator
instance withofFloat
. -
int
if you do it withofInt
. -
ofObject
is for the cases whenfloat
orint
is not enough — it’s often used to animate colors.
You can also animate any property of View
. Some examples are:
-
scaleX
andscaleY
– these allow you to scale the view by the x-axis or y-axis independently, or you can call both with the same value to animate the view’s size. -
translationX
andtranslationY
– these allow you to change the view’s on-screen position. -
alpha
– animate view’s transparency; 0 stands for completely transparent and 1 for completely opaque. -
rotation
– rotates the view on screen; the argument is in degrees, so 360 means a full clockwise turn. You may specify negative values as well, for instance, -90 means a counterclockwise quarter-turn. -
rotationX
androtationY
– the same asrotation
but along the x-axis and y-axis. These properties allow you to rotate in 3D. -
backgroundColor
– lets you set a color. The integer argument must specify a color as the Android constantsColor.YELLOW
andColor.BLUE
do.
ObjectAnimator
Meet ObjectAnimator
, a subclass of ValueAnimator
. If you only need to animate a single property of a single object, ObjectAnimator
may just be your new best friend.
Unlike ValueAnimator
, where you must set a listener and do something with a value, ObjectAnimator
can handle those bits for you almost automagically. :]
Go to LaunchRocketObjectAnimatorAnimationActivity.kt class and enter the following code:
// 1
val objectAnimator = ObjectAnimator.ofFloat(rocket, "translationY", 0f, -screenHeight)
// 2
objectAnimator.duration = DEFAULT_ANIMATION_DURATION
objectAnimator.start()
Here’s what you’re doing:
- Creating an instance of
ObjectAnimator
(like you did withValueAnimator
) except that the former takes two more parameters:-
rocket
is the object to animate. - The object must have a property corresponding to the name of the property you wish to change, which in this example is
“translationY”
. You’re able to do this becauserocket
is an object of classView
, which, in its base Java class, has an accessible setter withsetTranslationY()
.
-
- You set the duration for the animation and start it.
Run your project. Select Launch a rocket (ObjectAnimator) in the list. Tap on the screen.
The rocket behaves the same as it did with ValueAnimator
, but with less code. :]
Note: There’s a limitation to ObjectAnimator
— it can’t animate two objects simultaneously. To work around it, you create two instances of ObjectAnimator
.
Consider your use cases and the amount of coding required when you decide to use ObjectAnimator
or ValueAnimator
.
Note: There’s a limitation to ObjectAnimator
— it can’t animate two objects simultaneously. To work around it, you create two instances of ObjectAnimator
.
Consider your use cases and the amount of coding required when you decide to use ObjectAnimator
or ValueAnimator
.