Frame animation in android

August 27, 2012 | Rishabh Agrawal | General

Frame animation is explained below using a example that uses different loading images to creating a animation which continues for the specified time.

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 not started in OnCreate(), but 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 :


THE AUTHORRishabh Agrawal

Rishabh Agrawal is the founder of Creatiosoft, a company focused on creating high-quality software for the iGaming industry, specialising in poker and card games. With years of experience, Rishabh is dedicated to delivering engaging and user-friendly gaming experiences. Through this blog, he shares his passion and insights to help readers understand the latest trends and advancements in iGaming.

Recent Posts

The Future of Mobile Gaming: What to Expect in the Next 5 Years

Over the years, mobile gaming has seen amazing development and innovation utilized by the game development company in India. It…
06 Jun 2023 Rishabh Agrawal

How Do I Get Started With NFT Gaming?

The popularity of the NFT game has created a different fan base. There was a time when games were a…
18 Jul 2022 Rishabh Agrawal

What Are The Top Trends in NFT Marketplace 2022?

The NFTs are the new engaging and revolutionary technology across the globe. Though, these non-fungible tokens appeared for the first…
09 Jun 2022 Rishabh Agrawal