Frame animation in android

Frame animation is explained below using a example that uses different loading images to creating a animation which continues for the specified time.
An animation defined which shows a sequence of images in order (like a film).

android.graphics.drawable.AnimationDrawable class is used for animations

The AnimationDrawable class is the basis for loading drawable resources one after another to create an animation. Notice that the animation is started in class starter’s in public void run() .

Example:

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class SplashScreenActivity extends Activity
{
AnimationDrawable animation;
protected boolean _active = true;
protected int _splashTime = 5000;
@Override
public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
animation = new AnimationDrawable();

// the following code adds frames AnimationDrawable type animation and each frame //fetches the image from drawable ,which is fetched from resource folder. animation.addFrame(getResources().getDrawable(R.drawable.loading1_image), 100);
animation.addFrame(getResources().getDrawable(R.drawable.loading2_image), 100);
animation.addFrame(getResources().getDrawable(R.drawable.loading3_image), 100);
animation.setOneShot(false);// Sets whether the animation should play once or repeat

ImageView anim = (ImageView) findViewById(R.id.frame_id);
anim.setBackgroundDrawable(animation);// Set the background to a given Drawable
anim.post(new Starter());
/* a thread is being created */
Thread splashThread = new Thread() {
@Override
public void run()
{
try
{
int waited = 0;

while (_active && (waited < _splashTime))
{
sleep(1000);//waiting time
if (_active)
{
waited += 1000;
}
}
} catch (InterruptedException e)
{
}

finally {
finish();
);

//the next activity is called

Intent info = new Intent(SplashScreenActivity.this,SoundActivity.class
startActivity(info);
}
}
};
splashThread.start();
}
class Starter implements Runnable {
public void run() {
animation.start(); // Starts the animation
}
}
}

Steps:
• Create a Project .
• Create the xml file.
• Add some images into your drawable folder.
• Add the above code in your Activity class.
• Run the application.

Screen shots of the animation :

splashanim1 splashanim2

Advertisements

6 thoughts on “Frame animation in android

  1. Magnificent goods from you, man. I’ve understand your stuff previous to and you’re just extremely magnificent. I actually like what you’ve acquired here, really like what you’re stating and the way in which you say it. You make it entertaining and you still care for to keep it wise. I can’t wait to read far more from you. This is really a terrific website.

  2. Thanks for ones marvelous posting! I genuinely enjoyed reading it, you might be a great author.I will always bookmark your blog and will eventually come back later in life. I want to encourage continue your great job, have a nice weekend!

  3. I simply want to mention I am beginner to weblog and truly liked you’re blog. Likely I’m going to bookmark your site . You really come with great writings. Appreciate it for sharing your website.

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