Custom Animation while switching between Activities

It’s quite simple after all.
What you have to do is to call overridePendingTransition(int enterAnimation, int exitAnimation) immediately after startActivity(Intent) or finish() to specify an explicit transition animation to perform next.

You will have to create anim folder within res folder (if you already didn’t do it). In /res/anim/ add XML files that are representing single Animations.

Here are some common Animations that you can simply copy and paste separately into XML files:

/res/anim/left_out.xml

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

/res/anim/right_in.xml

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

/res/anim/bottom_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<translate
android:fromYDelta="100%p"
android:toYDelta="0"
android:duration="500"/>
</set>

/res/anim/top_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<translate
android:fromYDelta="0"
android:toYDelta="-100%p"
android:duration="500"/>
</set>

/res/anim/fade_in.xml

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="800"
android:repeatCount="0" />
</set>

/res/anim/fade_out.xml

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="800"
android:repeatCount="0" />
</set>

Duration, repeatCount, and other values are on your behalf to modify to your preference.

Now, all you have to do is to put this kind of line of code after startActivity(Intent) or finish()
overridePendingTransition(R.anim.right_in, R.anim.left_out);

Play with two Animation parameters until you get transition effect that suits you. Also, if you have another ideas for XML transitional files, post them in comments so we could add them to a list above.

Advertisements
Tagged , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: