Android transitions- slide in and slide out

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

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:
You can see that we have defined a layout using the ViewFlipper. In the ViewFlipper we've included two views; our first view and our second view. Initially ViewFlipper will show our first child view. We haven't defined these child views yet so lets do that. First we need to create our first view. In the res/layout directory create a new file called first_view.xml. Now add this xml to the file:
So our first view contains a few random components including a button that will allow us to navigate to our second view. So lets created our second view. Again we need to create a new xml file in the res/layout directory. This time we are going to call it second_view.xml. Edit this file so that it looks like this:
 
 
Creating the Slide animations

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:
 
To naivigate back to out first view from out second view we need another two animations, out to right and in from left:
 
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:
So we now have a working slide animation between two different views! Of course we can change the Animations to give us any transistion that we wish. Here we created our animations in Java but it is also possible to define them in XML, maybe more on that another time. Also these transistions are between views and not activities. It is possible to turn on a Slide transistion effect for activities from the settings menu.

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值