Thursday 5 September 2013

Animation between activity switching

For most of the android apps we do have multiple activities and when we start another activity either we get no animation (in older devices) or  a left-right translation. It is not so cool for a stylish app. 

So, the code to change the animation between two Activities is very simple: just call the overridePendingTransition() from the current Activity, after starting a new Intent. This method is available from Android version 2.0 (API level 5), and it takes two parameters, that are used to define the enter and exit animations of your current Activity. Here’s an example:

startActivity(new Intent(this, NewActivity.class));         
overridePendingTransition(R.anim.push_left_in,R.anim.push_out_up);   


These two parameters are resource IDs for animations defined with XML files (one for each animation). These files have to be placed inside the app’s res/anim folder. 

push_left_in.xml:-


 <?xml version="1.0" encoding="utf-8"?>   
 <set xmlns:android="http://schemas.android.com/apk/res/android">   
  <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="300"/>   
  <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />   
  </set>   

push_out_up.xml:-

 <?xml version="1.0" encoding="utf-8"?>   
 <set xmlns:android="http://schemas.android.com/apk/res/android">    
 <translate android:fromXDelta="100%p" android:fromYDelta="100%p" android:toXDelta="0" android:toXDelta="0" android:duration="300"/>   
 </set>   

The first xml code makes slides the View diagonally. The second one slides the View to the left and then up. 

No comments:

Post a Comment