How to Perform Image Animation in Android

In this tutorial how to perform Image Animation in Android is shown.

Point to Remember:

  • Add animation.xml file under the same file where all your images for the animation are located, as in my case it is under ‘res/drawable-hdpi’ folder.
  • If you want your animation to repeat continuously then keep the ‘android:oneshot’ property of the <animation-list> to false.

Code:

activity_main.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” >

<ImageView
android:id=”@+id/imageView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”211dp” />

</RelativeLayout>

res/drawable-hdpi/animation.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<animation-list xmlns:android=”http://schemas.android.com/apk/res/android”
android:oneshot=”false”>
<item android:drawable=”@drawable/img1″
android:duration=”120″/>
<item android:drawable=”@drawable/img2″
android:duration=”120″/>
<item android:drawable=”@drawable/img3″
android:duration=”120″/>
<item android:drawable=”@drawable/img4″
android:duration=”120″/>
<item android:drawable=”@drawable/img5″
android:duration=”120″/>
<item android:drawable=”@drawable/img6″
android:duration=”120″/>
<item android:drawable=”@drawable/img7″
android:duration=”120″/>
<item android:drawable=”@drawable/img8″
android:duration=”120″/>
<item android:drawable=”@drawable/img9″
android:duration=”120″/>
<item android:drawable=”@drawable/img10″
android:duration=”120″/>

</animation-list>

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.widget.ImageView;

public class MainActivity extends Activity{

ImageView anm;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
anm=(ImageView)findViewById(R.id.imageView1);
anm.setBackgroundResource(R.drawable.animation);
}

public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
AnimationDrawable frameAnimation=(AnimationDrawable)anm.getBackground();
if(hasFocus)
{
frameAnimation.start();
}
else
{
frameAnimation.start();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

https://youtu.be/oWjABm-cvmo

Add a Comment

Your email address will not be published. Required fields are marked *