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

Zoomin animation on picture in android

Zoomin animation falls under the category of View Animation. The view animation framework animations, which can both be declared in XML.

The  zoomin animation xml file is given below :

Zoomin.xml


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/decelerate_interpolator">
 <scale android:fromXScale="2.0" android:toXScale="1.0"
 android:fromYScale="2.0" android:toYScale="1.0"
 android:pivotX="50%p" android:pivotY="50%p"
 android:duration="@android:integer/config_longAnimTime" />
</set>
  • <set> contains other animation elements.
  • <scale > is a  resizing animation tag. You can specify the center point of the image from which it grows outward (or inward) by specifying pivotX and pivotY.
  • fromXScale/fromYScale represents the initial state and toXScale/toYScale represent the final state .

Hence , in this example the image is zoomed to its double size as “1.0” scale specifies no change.

Note: The zoomin.xml is created in anim folder which is a part of res folder.

The code for loading and starting the animation is given below.

 


public void onItemClick( View v)

{

Animation animLinear = AnimationUtils.loadAnimation(mContext, R.anim.zoomin);

v.startAnimation(animLinear);//starts the animation

}

android.view.animation.AnimationUtils  defines common utilities for working with animations and loadAnimation .loads an animation object from a resource anim folder.

Hence ,create the animation xml file first and then add the above code

Srcreenshots:

Screenshot1                   Screenshot 2                 Screenshot 3