我想获得用户按下屏幕(ACTION_DOWN)和释放屏幕(ACTION_UP)之间的距离。这是为了能够将位图图像移动相同的量(即使它被推到屏幕外),而不会导致它跳回光标位置(或回到屏幕上)。但是,我的代码有时似乎会将图像移动到错误的方向,或者即使我释放屏幕按下也会导致它继续移动。这里是否存在我需要考虑的去抖动问题?
我的 onTouch 方法(获取 ACTION_DOWN 点和 ACTION_UP 点,并减去一个)
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;
}
还有我的 run() 方法
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);
}
}
谢谢