The example in this article describes how Android implements moving pictures on the screen. Share to everyone for your reference. The implementation methods are as follows:
1. Speed.java Documents:
Package net.obviam.droidz.model.components;
public class Speed {public static final int direction_right = 1;
public static final int direction_left =-1;
public static final int direction_up =-1;
public static final int direction_down = 1; Private float XV = 1; Velocity value on the X axis private float YV = 1;
Velocity value on the Y axis private int xdirection = Direction_right;
private int ydirection = Direction_down;
Public Speed () {THIS.XV = 1;
THIS.YV = 1;
Public Speed (float XV, float yv) {this.xv = XV;
This.yv = YV;
public float GETXV () {return XV;
public void Setxv (float xv) {this.xv = XV;
public float Getyv () {return yv;
public void Setyv (float yv) {this.yv = YV;
public int getxdirection () {return xdirection;
The public void setxdirection (int xdirection) {this.xdirection = xdirection;
public int getydirection () {return ydirection; } public void Setydirection (int ydirection) {this.ydirection = ydirection;
}//Changes the direction on the X axis public void togglexdirection () {xdirection = xdirection *-1;
}//Changes the direction on the Y axis public void toggleydirection () {ydirection = ydirection *-1; }
}
2. Main.java File:
public void Run () {Canvas Canvas;
LOG.D (TAG, "Starting game loop");
while (running) {canvas = null; Try locking the canvas for exclusive pixel editing//In the surface try {canvas = This.surfaceHolder.lo
Ckcanvas ();
Synchronized (Surfaceholder) {//Update game State this.gamePanel.update ();
Render state to the "screen//Draws" canvas on the panel this.gamePanel.render (canvas); finally {//In case of ' an exception the ' surface is ' not ' in//a inconsistent state if (can
VAS!= null) {surfaceholder.unlockcanvasandpost (canvas); }//End finally} is public void Update () {//Check collision and right wall if heading right if (droid.get Speed (). getxdirection () = = Speed.direction_right && droid.getx () + Droid.getbitmap (). GetWidth ()/2 >= ge
Twidth ()) {droid.getspeed (). Togglexdirection (); }//Check collision with leftWall if heading left if (Droid.getspeed (). getxdirection () = = Speed.direction_left && droid.getx ()-Droid.
Getbitmap (). GetWidth ()/2 <= 0) {droid.getspeed (). Togglexdirection ();
}//Check collision with bottom wall if heading down if (Droid.getspeed (). getydirection () = = Speed.direction_down && droid.gety () + Droid.getbitmap (). GetHeight ()/2 >= getheight ()) {droid.getspeed (). Toggleydirection (
); }//Check collision with top wall if heading up if (Droid.getspeed (). getydirection () = = Speed.direction_up
;& droid.gety ()-Droid.getbitmap (). GetHeight ()/2 <= 0) {droid.getspeed (). Toggleydirection ();
}//Update the Lone Droid droid.update (); }
I hope this article will help you with your Android program.