In this article by Alex Shaw, we will build on tweening techniques and also apply some new techniques that were introduced in Android 3.0.
Posted by filter-coffee on Jan 27th, 2012
Basic Animation.
In this article by Alex Shaw, author of Android 3-0 Animations Beginners Guide, we will build on the tweening techniques we've already learned, and also apply some new techniques that were introduced in Android 3.0.
In this article, we shall:
Note for developers using versions of Android before 3.0 So far, everything we have learned has been backwards-compatible with previous versions of Android. This will hold true for the first part of this article, but not the second. That is to say that ViewFlippers are backwards-compatible with previous versions of Android, but ValueAnimators and ObjectAnimators are new in version 3.0.
At the time of writing (mid-2011), the Android Compatibility Package does not help with this problem.
Turning pages with a ViewFlipper ViewFlipper is a neat little wrapper class for applying a page-turning animation to a set of pages. It makes use of the tween animation classes, and extends them with an XML interface.
The ViewFlipper is actually a subclass of something called a ViewAnimator. Do not get confused! A ViewAnimator is a completely different class to a ValueAnimator or an ObjectAnimator, and they are not interchangeable.
Let's see more.
You have been hired by a children's book publisher to make an interactive book. The book will teach kindergarten children about different sorts of motion by showing them small animations on the pages.
First up, we will use a ViewFlipper widget to make an animated page-turning interface. What better way to learn about a page-turning widget than by using it to make a book? We will also add some simple pages to test the ViewFlipper, which we can add animations to in some later examples.

<!--?xml version="1.0" encoding="utf-8"?-->
<!--?xml version="1.0" encoding="utf-8"?-->



import android.view.View; import android.widget.Button; import android.widget.ViewAnimator;
final ViewAnimator pages = (ViewAnimator) findViewById(R.id.pages); Button prev = (Button) findViewById (R.id.prev); Button next = (Button) findViewById (R.id.next); prev.setOnClickListener(new View.OnClickListener() { public void onClick (View v) { pages.showPrevious(); } }); next.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { pages.showNext(); } });

<!--?xml version="1.0" encoding="utf-8"?-->
<!--?xml version="1.0" encoding="utf-8"?-->

We created a book-like application that displays several pages of information. We created a new ViewFlipper widget and applied a page-turning animation to it to give it a natural, book-like feel.
For convenience, the animations applied to ViewFlipper will apply to every single page that is contained within it. Remember, you do not need to apply an individual tween to each page in your book. Just adding the inAnimation and outAnimation in your ViewFlipper will be sufficient.
(For more resources on Android, see here.)
Think about how you would like to turn pages in a book. Perhaps the motion that we created above could be improved in some way.
Edit the slidein.xml and slideout.xml tween animations, and create a new animation of your own invention.
Creating tween animations in Java So far, all of the tween animations we made have been created in XML, and there is good reason for this. Why should you want to clutter up your logical code with a load of presentation code?
But sometimes you want to create your tweens programmatically, perhaps because they rely on some computed values or it makes sense to describe them computationally.
Whatever the reason, we can use Java to create tween animations just as easily as we can create them in XML.
We want to make a new animation to replace slidein.xml. This time, we want our pages to come in from the right, as before, but we will add a scale animation too, to make it look more exciting. It will be as if the page is being pulled from a tall stack of pages, just out of view.
But we're bored of XML. Don't ask me why, perhaps it's because of all those pointy brackets. Give us the round parentheses of Java, we say! We will use the Java equivalent of the XML tags for , , and animations.
import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation;
AnimationSet slideAndScale = new AnimationSet(true);
TranslateAnimation slide = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 1f, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0 );
ScaleAnimation scale = new ScaleAnimation( 10, 1, 10, 1 );
as follows:
slideAndScale.addAnimation(slide); slideAndScale.addAnimation(scale);
slideAndScale.setDuration(1000);
pages.setInAnimation(slideAndScale);

We've created a new page-turning animation, which is an AnimationSet, containing a ScaleAnimation and a TranslateAnimation. Now the page looks like it is being lifted into view, as it is turned.
We've created tween animations before, but this one was in Java. We have seen that it is possible to create a tween animation in Java that provides the same sort of functionality, which you would expect from a tween animation created in Java. By comparing the source against its equivalent in XML, you can see where the differences lie.
Writing the SlideAndScale animation in Java In Java, we instantiate the AnimationSet, ScaleAnimation, and TranslateAnimation. The animation objects are parameterized in their respective constructors.
We then add the ScaleAnimation and TranslateAnimation to the AnimationSet.
AnimationSet slideAndScale = new AnimationSet(true); TranslateAnimation slide = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 1f, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0 ); ScaleAnimation scale = new ScaleAnimation( 10, 1, 10, 1 ); slideAndScale.addAnimation(slide); slideAndScale.addAnimation(scale); slideAndScale.setDuration(1000); Writing the SlideAndScale animation In XML In XML, the tween animation is created by declaring a tag that contains the translate and scale operations as child nodes. The animations are parameterized by giving them attributes.
<!--?xml version="1.0" encoding="utf-8"?--> As you can see, the advantage of the XML version is that it is more clearly laid-out. This is not just a matter of personal taste; by writing each attribute name as you assign it, there is never any ambiguity as to which value you are assigning. Look at the Java version and see if you can remember what order the constructor arguments are constructed in. It's hard, isn't it?
In conclusion, programmatic tween creation should only be used when you can think of a clear advantage.
(For more resources on Android, see here.)
Okay, now that we've made a tween that scales and slides in the graphic, have a go at making a similar tween for the outAnimation part of the interactive book.
Look at the code you've already written, and make a new animation in Java with the following properties:
Animating with ObjectAnimator ObjectAnimators are the first animations you will learn about, which are new in Android 3.0. Recall that tweens are all about moving views from one place to another, and that they describe different kinds of motion. Unlike tweens, animators work by updating values on an object in a much more programmatic way.
Animators just change numeric parameters on an object that they know nothing about, for instance, translating a view from one place to another. But by applying an animator to the X and Y coordinates of a view, an Animator can be used to perform the same task that a tween would do.
Which one you choose is up to you. Animators give you more flexibility, but they might not be as clear to read.
The children's book company has got back to us and they're not happy with the first page of our interactive book. It says that the ball is rolling, but it isn't! We're going to fix this by using an ObjectAnimator to move the ball backwards and forwards across the screen.
View rollingBall = findViewById(R.id.rollingball); ObjectAnimator ballRoller = ObjectAnimator.ofFloat( rollingBall, "TranslationX", 0, 400 );
ballRoller.setDuration(2000); ballRoller.setRepeatMode(ValueAnimator.REVERSE); ballRoller.setRepeatCount(ValueAnimator.INFINITE);

Here we used our first Animator, and it is an ObjectAnimator. The ObjectAnimator provides a simple way to animate a scene by continuously updating a parameter of that scene.
In this article, we have made an interactive animation using the Android ViewFlipper class to make a page-turning interface. We also learned about the ValueAnimator and ObjectAnimator classes, which are new animation techniques in Android 3.0.
Summary In this article, we learnt about the ViewFlipper class, ValueAnimator and the ObjectAnimator classes.
(For more resources on Android, see here.)
Bring your Android applications to life with stunning animations.
Only registered members can share their thoughts. So come on! Join the community today (totally free) and do things you never thought possible.