I’m trying to move bitmap picture from some point to point where I touched… but things not going well.
Render.java (extends Thread class)
public void run() {
Canvas canvas;
while (runFlag) {
long now = System.currentTimeMillis();
long elapsedTime = now - prevTime;
if (elapsedTime > 30){
prevTime = now;
if (touched ==true) {
matrix.postTranslate(touched_x, touched_y);
touched =false;
}
}
canvas = null;
try {
canvas = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
if (touched ==true) {
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(picture, matrix, null);
touched = false;
}
}
}
finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
Here I create a flag touched to check it when drawing… seems it is not good idea.
View.java (SurfaceView class)
public boolean onTouchEvent(MotionEvent event) {
touched_x = event.getX();
touched_y = event.getY();
int action = event.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
touched = true;
break;
case MotionEvent.ACTION_MOVE:
touched = true;
break;
case MotionEvent.ACTION_UP:
touched = false;
break;
case MotionEvent.ACTION_CANCEL:
touched = false;
break;
case MotionEvent.ACTION_OUTSIDE:
touched = false;
break;
default:
}
return false;
}
i think there is no persistence in your code for the image you are trying show. the code looks like it in one frame and erases it the very next. use an array or list that holds all the images that you want to draw and loop through that in the draw method.