Android provides a large number of classes and interface for the animation development. Most of the classes and interfaces are given in android.animation package.

Android Animation enables you to change the object property and behavior at run time. There are various ways to do animation in android.

The AnimationDrawable class provides methods to start and end the animation. Even, you can use time based animation.

Let’s have a look at the simple example of android animation.

activity_main.xml

You need to have a view only.

File: activity_main.xml
 
    
    
  
      
  
 
File: logo.xml

Have a image view only.

  
  
  
  

MainActivity class

File: MainActivity.java
package com.javatpoint.animation;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.graphics.drawable.AnimationDrawable;  
import android.widget.ImageView;  
  
public class MainActivity extends Activity {  
  
    ImageView anm;  
     public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.logo);  
            anm = (ImageView)findViewById(R.id.anm);  
              
            anm.setBackgroundResource(R.drawable.animation);  
        // the frame-by-frame animation defined as a xml file within the drawable folder  
              
            /* 
             * NOTE: It's not possible to start the animation during the onCreate. 
             */  
        }  
     public void onWindowFocusChanged (boolean hasFocus) {  
            super.onWindowFocusChanged(hasFocus);  
            AnimationDrawable frameAnimation =   
                (AnimationDrawable) anm.getBackground();  
            if(hasFocus) {  
                frameAnimation.start();  
            } else {  
                frameAnimation.stop();  
            }  
        }  
  
}

You need to create animation.xml file inside res/drawable-hdpi directory.

You need to have many images. Here, we are using 14 images and all the 14 images are located inside res/drawable-mdpi directory.

File: animation.xml
  
  
  
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
  

Output:

android animation example output 1 android animation example output 2