I've recently been looking at the android platform and having some fun with writing some small apps. I wanted to do some interesting transitions between views, and decided to have a go at a transition were one view slides off to the left and the other sides in from the right and vice versa. Here's a video showing an example of the end result:
To do this I created a simple project in eclipse using the Android plugins and made use of the ViewFlipper. So the first thing we need to do is create a new Android project called ViewFlipper, we'll call the application and activity Slide and define the package name to be com.example.viewflipper
Now we have all the layouts and views completed for our viewFlipper app'. So lets take a look at how to add the animations
and the listener functions for the buttons. ViewFlipper extends ViewAnimator which allows us to specifiy two animations, the out animation and the in animation. From out first view, when we click the button, we want this view to slide out to the left of the screen and the new view to slide in from the right of the screen. We are going to specify these two animations in our main activity class which is called Slide.java Here's the two Animations for achieving this:
Now that we have all out animations setup we just need to add the click listeners for each of our buttons. We'll add these to the onCreate method. In our click listeners we have to set the in and the out animations, this is to ensure that we call the right animations when going from the first to second and vice versa. Last, but not least, we also have to tell the view flipper which view to show when we click the buttons. To do this we use the showNext and showPrevious methods of our viewflipper. The full Slide.java class should now look like this:
Define a layout with a ViewFlipper
First of all we are going to edit the main.xml layout file in the res/layout directory of the our new android application here it is:
Creating the Slide animations
To naivigate back to out first view from out second view we need another two animations, out to right and in from left:
So that's it, have fun with the ViewFlipper and Animations, it would be great to here if anyone has seen any other interesting ways of creating transistions.