i have bitmap moves to right inside canvas i want when this bitmap closes to the right border of screen i.e : x position < getWidth() with certain small value i want to translate canvas co-ordinates so that the canvas itself moves also along side the movement of the bitmap inside it
another meaning ; i want the bitmap continue in moving to right permanently and of course when it closes to the right border of screen it will disappear soon so i want the canvas itself to move with me to make the bitmap visible always and i want to know how to set the x position value after translate canvas that
i.e : something such as in all games that has a character moves to right and the screen moves also with it .
this is the code that found here
http://www.edu4java.com/en/androidgame/androidgame3.html
and i edited it and now the bitmap repeats its movement when it closes to the right border of the screen .
MyActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GameView(this));
}
and MyGameView (the main class of my problem )
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GameView extends SurfaceView {
private Bitmap bmp;
private SurfaceHolder holder;
private GameLoopThread gameLoopThread;
private int x = 0;
private int xSpeed = 2;
public GameView(Context context) {
super(context);
gameLoopThread = new GameLoopThread(this);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
gameLoopThread.setRunning(false);
while (retry) {
try {
gameLoopThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.attention);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(x==getWidth())
{
Log.e("cds","cdca");
canvas.translate(x, 0);
x=10;
}
else
{
x = x + xSpeed;
}
if(canvas!=null) {
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(bmp, x, 10, null);
}
}
}
GameLoopThread .java
import android.graphics.Canvas;
public class GameLoopThread extends Thread {
static final long FPS = 10;
private GameView view;
private boolean running = false;
public GameLoopThread(GameView view) {
this.view = view;
}
public void setRunning(boolean run) {
running = run;
}
@Override
public void run() {
long ticksPS = 1000 / FPS;
long startTime;
long sleepTime;
while (running) {
Canvas c = null;
startTime = System.currentTimeMillis();
try {
c = view.getHolder().lockCanvas();
synchronized (view.getHolder()) {
view.onDraw(c);
}
} finally {
if (c != null) {
view.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime = ticksPS-(System.currentTimeMillis() - startTime);
try {
if (sleepTime > 0)
sleep(sleepTime);
else
sleep(10);
} catch (Exception e) {}
}
}
}