Sunday, January 29, 2012

How to use animation between two acitivities i.e. transition ?


Here's the code to do a nice smooth fade between two Activities..
Create a file called fadein.xml in res/anim
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
   android:interpolator="@android:anim/accelerate_interpolator"
   android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" />
Create a file called fadeout.xml in res/anim
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
   android:interpolator="@android:anim/accelerate_interpolator"
   android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="2000" />
If you want to fade from Activity A to Activity B, put the following on button click
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
If the fades are too slow for you, change android:duration in the xml files above to something smaller.
button_click.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent mainIntent = new Intent(this,B.class);
ctx.startActivity(mainIntent);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}
}, 900);
}
});

No comments:

Post a Comment