Monday, April 25, 2011

Flip Animation in Android

I wanna share a piece of code for implementing flip animation in android. Forgive me for any mistakes. I hope it will help many.

To run this you will have to create an xml file under folder called "anim" of your project and paste the below code.

For flipping upwards:

grow_from_down.xml


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/linear_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.0"
        android:fromYScale="0.0"
        android:toYScale="1.0"
        android:pivotY="50%"
        android:fillAfter="false"
        android:startOffset="200"
        android:duration="200" />
    <translate
        android:fromYDelta="100%"
        android:toYDelta="0"
        android:startOffset="200"
        android:duration="200"/>
</set>


For flipping downwards:

shrink_to_down.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/linear_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.0"
        android:fromYScale="1.0"
        android:toYScale="0.0"
        android:pivotY="100%"
        android:fillAfter="false"
        android:duration="200" />
    <translate
        android:fromYDelta="1.0"
        android:toYDelta="100%"
        android:duration="300"/>
</set>

To load these animations, code the lines below:

Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.grow_from_down);
layout.startAnimation(anim);

Enjoy the flipping animation :)