I want to animate fishes….Following are the steps to be followed:
fish class…
public class Jellyfish {
public float x; // x co-ordinate
public float y; // y co-ordinate
public float off;
ValentineEngine engine;
Bitmap[] bitmap;
int currentFrame; // current frame display..
long frameTicker;
int frameNum; // no of frame
// public float mX;
// public float mY;
double speedX; // x co-ordinate speed of animation
double speedY; // y co-ordinate speed of animation
int direction; // Direction of animaton
int[] FPS;
int[] images = new int[] { R.drawable.jelly_fish1, R.drawable.jelly_fish2,
R.drawable.jelly_fish3, R.drawable.jelly_fish4,
R.drawable.jelly_fish5, R.drawable.jelly_fish6,
R.drawable.jelly_fish7, R.drawable.jelly_fish8,
R.drawable.jelly_fish9, R.drawable.jelly_fish10,
R.drawable.jelly_fish11, R.drawable.jelly_fish12 };
void init() {
frameNum = images.length; // array size
bitmap = new Bitmap[frameNum];
FPS = new int[] { 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185 };
}
public Jellyfish(Resources res, float x, float y,
ValentineEngine valentineEngine, double speedX, double speedY, // and getting value of all perameter…
int direction) {
this.direction = direction;
this.speedY = speedY;
this.speedX = speedX;
this.engine = valentineEngine;
init();
for (int i = 0; i < frameNum; i++) {
bitmap[i] = BitmapFactory.decodeResource(
}
this.x = x;
this.y = y;
}
/* method to draw fishes on canvas */
public void doDraw(Canvas canvas) {
//off = -(engine.width * engine.xOffset) + x;
canvas.drawBitmap(bitmap[
}
/* method to animate fishes */
public void animate(long elapsedTime) {
if (System.currentTimeMillis() > frameTicker + FPS[0]) {
currentFrame++;
if (currentFrame >= frameNum) {
currentFrame = 0;
}
frameTicker = System.currentTimeMillis();
x += speedX * (elapsedTime / 20f); // updating X & Y co-ordinate
y += speedY * (elapsedTime / 20f) * direction;
}
}
/* method to check border when they cross the border */
public boolean checkBorder() {
if (direction == -1)
if (y < 0)
return true;
else if (y > engine.height)
return true;
return false;
}
}
/***** in your wallpaper services class*******/
create arraylist
ArrayList<Jellyfish> jellyfish;
jellyfish = new ArrayList<Jellyfish>();
/*****in animate method in your wallpaper services class********/
synchronized (jellyfish) {
if (jellyfish.size() < 3) {
/* calling jellyfish constructor */
addjellyFish();
}
for (int i = 0; i < jellyfish.size(); i++) {
if (!jellyfish.get(i).
/* animate jellyfish */
jellyfish.get(i).animate(
} else {
/* fishes is removed when they cross the border */
jellyfish.remove(i);
}
}
}
/***** in doDraw method of wallpaper services class********/
synchronized (jellyfish) {
for (int i = 0; i < jellyfish.size(); i++) {
/* drawing fish on to the canvas */
jellyfish.get(i).doDraw(
}
}
/***** addjelliFish() metod call in animate metod of wallpaper services class********/
/* jellyfish constructor calling*/
void addjellyFish() {
synchronized (jellyfish) {
jellyfish.add(new Jellyfish(getResources(), (float) getRandom(
0, width – 150),
(float) getRandom(height – 10, height), this,
getRandom(2.23, 2.35), getRandom(2.25, 2.45), -1));
}
}
-By Sonu Odesi
Android developer
Comments are closed.