How to use Custom Transition Animation in Android

Android has a set of Animation API that makes you easily create custom animations in your Android application. You can create animations programmatically via Java code or defined them in xml files inside /res/anim folder. Using your custom animation, you can override the default transition animation between activities to make your app looks more elegant. Overriding the default transition animation is easy, just create two custom animations and call the overridePendingTransition method of Activity class to use them.

 

First you need to create two animation xml files one for incoming activity and another for outgoing activity

File 1 Incoming.xml

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:interpolator="@android:anim/accelerate_interpolator"
        android:fromXDelta="0"
        android:toXDelta="0"
        android:duration="300"/>

    <scale
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromXScale="0"
        android:fromYScale="0"
        android:toXScale="1.0"
        android:toYScale="1.0"
        android:startOffset="300"
        android:duration="300" />
</set>

File 2 Outgoing.xml

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:fromXDelta="0"
        android:toXDelta="-50%p"
        android:duration="300" />

    <scale
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale=".1"
        android:toYScale=".1"
        android:startOffset="300"
        android:duration="400" />
</set>

Save these two files in res/anim folder

The next step is to Call method overridePendingTransition (int enterAnim, int exitAnim) right after startActivity(intent) to override default transition animation between current activity and next activity.

Intent intent = new Intent(this, NextActivity.class);

startActivity(intent);
overridePendingTransition (R.anim.incoming, R.anim.outgoing)

Thats it and you are done.

Advertisements

What you think ? 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