You can animate just about anything in Android. For any animation to work, you minimally have to declare starting and ending values, and then calculate the changes to that value over time. You could do this manually, but there’s a class named ValueAnimator that will do this for you. The ValueAnimator class doesn’t actually execute animation of visual objects. Instead, it just figures out the changes in a value over some duration, and reports those changes to an update listener object. It’s up to you to listen for the updates and react.
- [Voiceover] You can animate just about anything in Android. For any animation to work, you minimally have to have starting and ending values and then calculate the changes to that value over time. You can do this manually, as you do with frame animations, or automatically, as you do with view animations, but there's another class named value animator that will calculate the values for you. The value animator class doesn't actually execute animation of visual objects. Instead, it just figures out the changes in a value over some duration and reports those changes to an update listener object.
It's up to you to listen for the updates and react. I'll demonstrate this in the application value animator. This is a version of an application that I built for a previous course on working with Java in Android named Code Runner. It's a very simple app. It just has a couple of buttons and a text view that I can use to log output. When I click the Run Code button, it simply displays a message and when I click Clear, it clears the output. Now I'm going to use this app to demonstrate the value animator class.
I'll put all of the code for this demonstration in the On Run button click method that's executed when I click the Run button. First I'll create an instance of the value animator class. Notice that there's also a value animator compat. That's an interface that's a part of this support library that doesn't actually have an implementation. I'll name the object animator and I'll instantiate it by calling a method of the value animator class named of float.
Notice that there's an of float and an of int and also one named of arg b for arbitrary types. If you choose of float, then you need to provide at least two values. If you provide two values, those will be the starting and ending values for the animation. I'll pass in two floating values of 1f and 20f. I don't really need the f's there because the of float method is expecting floating values, but this will just make the code a bit more readable.
Then, immediately, I'll call the method set duration. Notice again that there are many other set methods that you could use. I'll pass in a value of 2000. That's 2000 milliseconds or two seconds. Now before I run the animation, I want to listen for the updates and I'll do that by calling animator.add update listener and I'll pass in an implementation of an interface named animator update listener.
Android Studio expands the implementation to implement the on animation update method and that receives the reference to the value animator object. Within that method, I'll call my display message local method and this is what I'm using to output strings on my screen and I'll pass in two values. First I'll pass in a label of time stamp and I'll append to that animation.get current play time. That will return an integer value representing the number of milliseconds since the animation started.
Then I'll append another label comma value and then I'll pass in animation.get animated value and this is the value of the animation at this time stamp and then finally, I'll start the animation by calling animator.start and I'll run the new version of the application. I'll click the Run Code button and there's the my value animation. Notice that the updates are issued irregularly. That is, there isn't a fixed number of milliseconds between one update and the next, but they're regular enough to depend on for visual animations.
On each update, I get a revised value starting with one and going all the way up to my maximum value of 20. Also notice that the time stamp can exceed the maximum. I said my duration would be 2000, but my last time stamp was at 2002. The animator does this to make sure that it evens out the animations. I'll click Clear and I'll run the code again and once again there's the result. Now that could be enough code to get you started for your own particular animation, but there are other things you can do with the value animator class.
First of all, you can set repeats. Here I'm going to say animator.set repeat count and I'll set it to a value of two and I do this before I call the start method and then I'll run the code again and this time when the animation starts it runs twice. There's the first animation starting from the top of the screen. Then I'll go down to the end of that animation at 2000 milliseconds or so and I see that the animation has started again and I go down to the end and I'll actually see that the animation has run three times so if you set a repeat value of two, that means you're adding to the original animation.
Now I'll also set a value called the repeat mode with animator.set repeat mode and I'll pass in a constant of value animator.reverse. That means for each animation, it'll go forward and then back and then forward and back again. If I set my repeat count to two, that means I have three iterations of the animation in total. The first one will go forward, the second one in reverse, and the third forward again and I'll run the code and show that that's exactly what happens.
I'll go to the top of the screen and that's my first animation. Going through 2000 milliseconds or so and then I see when the second animation happens, that it's starting at the maximum value of 20 and then counting down and then for the second repeat or the third iteration, it's once again moving forward. Finally I'll show you a way of setting your animation to repeat over and over again, for as long as you like. I'll comment out that line of code so I'm not using repeat mode anymore and when I call set repeat count, I'll pass in value animator.infinite and that means run this animation in an infinite loop and I'll run the code and it looks like nothing's happening but now I'll clear the output and I'll see that the animation is continuing to run and right now, the only way of really stopping the animation is by closing the app completely and removing it from memory.
So that's a look at the value animator class. Again, you aren't actually animating any visual objects with this code. You're just calculating the changes in a value over time, but you can easily apply these changes to specific objects using a subclass of the value animator class named object animator and I'll demonstrate that in another video.
- Choosing an animation technique
- Defining and running view animations
- Animating with frames
- Animating object properties
- Animating transitions between activities and slides
- Using the transitions framework