Wednesday 12 October 2011

Simpe 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;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

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();
        paint.setColor(color);
        this.direction = direction;
    }
   
    public void move(Canvas canvas ) {
        if( direction.equals("up") ) {
            y--;
        }
       
        if( direction.equals("right") ) {
            x++;
        }
        if( direction.equals("down") ) {
            y++;
        }
        if( direction.equals("left") ) {
            x--;
        }
       
        draw(canvas);
    }
   
    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.graphics.Canvas;
import android.graphics.Color;
import android.widget.ImageView;

public class MyImageView extends ImageView {

    Ball bup, bright, bdown, bleft;
   
    public MyImageView(Context context, int width, int height ) {
        super(context);
        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");
    }
   
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(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 android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;

public class Home extends Activity {
   
    MyImageView view;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        view = new MyImageView(this, getWindowManager().getDefaultDisplay().getWidth(), getWindowManager().getDefaultDisplay().getHeight() );
        setContentView(view);
       
        new MyBallThread().execute(null);
       
    }
   
    class MyBallThread extends AsyncTask<Object, Object, Object> {

        @Override
        protected Object doInBackground(Object... params) {
            for( int i = 0; i < 200; i++ ) {
                publishProgress(null);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return null;
        }
       
        @Override
        protected void onProgressUpdate(Object... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
            view.invalidate();
        }
       
    }

}
 

------------------------------------------------

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

  "Pioneers in Mobile Computing Training"
  
   Find more about us at http://www.bitcode.in