Simple Animation Demo in Android, Using Multithreading

Hi All,
This is a sample application demonstrating how to Animate using AysncTask.
Ball class will draw itself by moving in a particular direction.
Ball class will hold it””””s radius, x y co-ordinates , color and the direction in which it should move. The move method of class Ball will manage the x,y co-ordinates depending on it””””s direction. The following is the class Ball .
package in.bitcode.animationdemo;
public class Ball {
    int x, y, r;
    Paint paint;
    String direction;
    public Ball( int x, int y, int r, int color, String direction ) {
        this.x = x; this.y = y; this.r = r;
        paint = new Paint();
        this.direction = direction;
    public void move(Canvas canvas ) {
        if( direction.equals(“up”) ) {
        if( direction.equals(“right”) ) {
        if( direction.equals(“down”) ) {
        if( direction.equals(“left”) ) {
    public void draw( Canvas canvas ) {
        canvas.drawCircle(x, y, r, paint);


MyImageView class
MyImageview class is extending the class ImageView.
It will contain four Ball objects, initialized to move up, right, down and left. Their initial position will be center of the screen.  It””””s onDraw method is overridden which will call the move method on each ball by passing it the Canvas object.
We will be having a Thread which will invalidate this MyImageView after some interval so that the onDraw of MyImageView is called which will in turn call the move method on each ball contained inside it.
Following is the class MyImageView:

package in.bitcode.animationdemo;

import android.content.Context;
import android.widget.ImageView;

public class MyImageView extends ImageView {

Ball bup, bright, bdown, bleft;

public MyImageView(Context context, int width, int height ) {
bup = new Ball( width/2, height/2, 20, Color.RED,”up”);
bright = new Ball( width/2, height/2, 20, Color.GREEN,”right”);
bdown = new Ball( width/2, height/2, 20, Color.BLUE,”down”);
bleft= new Ball( width/2, height/2, 20, Color.YELLOW,”left”);

protected void onDraw(Canvas canvas) {
bup.move( canvas );
bright.move( canvas );
bdown.move( canvas );
bleft.move( canvas );


The Home class:

Home class extends the Activity class. Home activity class will create an object of class MyImageView and set it as content view. It will have and inner class MyThread which will be extending the class AsyncTask. MyThread will invalidate the MyImageView after certain interval.

Following is the Home class, it is containing an inner class Mythread:

package in.bitcode.animationdemo;import;
import android.os.AsyncTask;
import android.os.Bundle;public class Home extends Activity {

MyImageView view;

protected void onCreate(Bundle savedInstanceState) {

view = new MyImageView(this, getWindowManager().getDefaultDisplay().getWidth(), getWindowManager().getDefaultDisplay().getHeight() );

new MyBallThread().execute(null);


class MyBallThread extends AsyncTask<Object, Object, Object> {

protected Object doInBackground(Object… params) {
for( int i = 0; i < 200; i++ ) {
try {
} catch (InterruptedException e) {
// TODO Auto-generated catch block
return null;

protected void onProgressUpdate(Object… values) {
// TODO Auto-generated method stub




–  Android @ BitCode Technologies Pvt. Ltd. – No. 1 Mobile  Training Company in Pune

“Pioneers in Mobile Computing Training”

Find more about us at

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>