I want to get the distance between the point where a user presses the screen (ACTION_DOWN) and where they release it (ACTION_UP). This is to be able to move the bitmap image the same amount (even if it gets pushed off-screen) without causing it to jump back to the cursor position (or back on-screen). However my code seems to move the image the wrong direction sometimes, or cause it to keep moving even when I release the screen press. Is there a debounce issue here that I need to account for?
My onTouch method (get ACTION_DOWN point and ACTION_UP point, and subtract one from other)
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if(action == event.ACTION_DOWN) {
preFingerPosX = event.getX();
preFingerPosY = event.getY();
}
else if(action == event.ACTION_UP) {
fingerPosX = event.getX();
fingerPosY = event.getY();
}
dx = fingerPosX - preFingerPosX;
dy = fingerPosY - preFingerPosY;
bmpX += dx;
bmpY += dy;
//invalidate();
return true;
}
And my run() method
public void run() {
// TODO Auto-generated method stub
while(isRunning) {
if(!surfaceHolder.getSurface().isValid()) {
continue;
}
canvas = surfaceHolder.lockCanvas();
canvas.drawRGB(0, 0, 0);
canvas.drawBitmap(bmp, bmpX-(bmp.getWidth()/2),
bmpY-(bmp.getHeight()/2), null);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
Thanks