Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create Animation using XML code in android?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 119
    Comment on it

    This tutorial will guide you to do animation in android. There are two ways to do animations in android , through XML code or java code. In this tutorial, we are using only first way through XML code. In this tutorial, we are defining animations like following .

    1. fade in
    2. fade out
    3. scale
    4. rotate
    5. slide up
    6. slide down

    Following are some steps to animation in android apps.

    Step1- First, create animation xml file

    We will create this animation file in res/anim/blink.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:repeatMode="reverse"
          android:repeatCount="infinite"/>
    </set>
    
    


    Step-2 Create a Activity and add following code

    public class MainActivity extends Activity{
    TextView textMessage;
    // Animation
    Animation animBlink;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blink);
        textMessage = (TextView) findViewById(R.id.textMessage);
        textMessag.setText(Welcome to Android Code Portal);
        // load the animation
        animBlink = AnimationUtils.loadAnimation(getApplicationContext(),
        R.anim.blink);
       // start the animation
        textMessage.startAnimation(animBlink);}
    }
    
    [Click and drag to move] 

    Here are some more important animations type:-

    1) Animation ZoomIn

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
       android:fillAfter="true" >
        <scale
           xmlns:android="http://schemas.android.com/apk/res/android"
           android:duration="1000"
           android:fromXScale="1"
           android:fromYScale="1"
           android:pivotX="50%"
           android:pivotY="50%"
           android:toXScale="3"
           android:toYScale="3" >
        </scale>
    </set>

     

    2) Animation ZoomOut

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
       android:fillAfter="true" >
        <scale
           xmlns:android="http://schemas.android.com/apk/res/android"
           android:duration="1000"
           android:fromXScale="1.0"
           android:fromYScale="1.0"
           android:pivotX="50%"
           android:pivotY="50%"
           android:toXScale="0.5"
           android:toYScale="0.5" >
        </scale>
    </set>

     

    3) Animation FadeIn

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
       android:fillAfter="true" >
        <alpha
           android:duration="1000"
           android:fromAlpha="0.0"
           android:interpolator="@android:anim/accelerate_interpolator"
           android:toAlpha="1.0" />
    </set>

     

    4) Animation FadeOut

    <set xmlns:android="http://schemas.android.com/apk/res/android"
       android:fillAfter="true" >
        <alpha
           android:duration="1000"
           android:fromAlpha="1.0"
           android:interpolator="@android:anim/accelerate_interpolator"
           android:toAlpha="0.0" />
    </set>

     

    5) Animation Rotate

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <rotate android:fromDegrees="0"
           android:toDegrees="360"
           android:pivotX="50%"
           android:pivotY="50%"
           android:duration="700"
           android:repeatMode="restart"
           android:repeatCount="infinite"
           android:interpolator="@android:anim/cycle_interpolator"/>
    </set>

     

    6) Animation Move

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

     

    7) Animation SlideDown

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
       android:fillAfter="true">
        <scale
           android:duration="800"
           android:fromXScale="1.0"
           android:fromYScale="0.0"
           android:interpolator="@android:anim/linear_interpolator"
           android:toXScale="1.0"
           android:toYScale="1.0" />
    </set>

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: