I have a Thread which draws my stuff with specific delay. Now I want to add to this stuff also method that will change drawn bitmap between bitmap[2]. This change bitmap delay I want to be different than thread's (thread's 33ms, bitmaps change 0.5s).
public class GameThread<T extends GameSurface> extends Thread {
private boolean isRunning;
private long startTime, loopTime;
private long delay = 33;
private SurfaceHolder surfaceHolder;
private T gameSurface;
public GameThread(SurfaceHolder surfaceHolder, T gameSurface) {
this.surfaceHolder= surfaceHolder;
this.gameSurface = gameSurface;
isRunning = true;
}
@Override
public void run() {
while(isRunning) {
startTime = SystemClock.uptimeMillis();
Canvas canvas = surfaceHolder.lockCanvas(null);
if(canvas != null) {
synchronized (surfaceHolder) {
(...)
if (TmxLoader.mapImage != null)
TmxLoader.drawBitmaps(canvas, gameSurface);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
loopTime = SystemClock.uptimeMillis() - startTime;
if(loopTime < delay) {
try {
Thread.sleep(delay - loopTime);
}
catch (InterruptedException e) {
Log.e("Interupted ex", e.getMessage());
}
}
}
}
}
fragment of TmxLoader.java
public static void drawBitmaps(Canvas canvas, GameSurface gameSurface) {
mapImageResized = new Bitmap[2];
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
int mapMainWidth = t.width;
int mapMainHeight = t.height;
mapImageResized[iter] = Bitmap.createScaledBitmap(mapImage[iter], gameSurface.getCellWidth() * mapMainWidth, gameSurface.getCellHeight() * mapMainHeight, false);
canvas.drawBitmap(mapImage[iter], 0, 0, PaintTemplates.getInstance(gameSurface.getContext()).pMap);
iter++;
if (iter == 2)
iter = 0;
}
}, 0, 500);
}
As you can see the thread's delay is 33ms, while Timer's delay is 0.5s. So the question is how to reconcile one delay with the other because now the Timer's delay (0.5s) seems not to work and now it 'uses' the 33ms delay.