fix: 修复滑动手势问题

This commit is contained in:
2026-07-14 00:38:46 +08:00
parent 86eaec3370
commit fc91e6b1f6

View File

@@ -22,6 +22,8 @@ public class RemoteTouchView extends SurfaceView {
private float lastTouchX; private float lastTouchX;
private float lastTouchY; private float lastTouchY;
private float lastAbsX;
private float lastAbsY;
private long swipeStartTime; private long swipeStartTime;
public interface TouchEventListener { public interface TouchEventListener {
@@ -49,8 +51,12 @@ public class RemoteTouchView extends SurfaceView {
public boolean onTouchEvent(MotionEvent event) { public boolean onTouchEvent(MotionEvent event) {
if (listener == null) return super.onTouchEvent(event); if (listener == null) return super.onTouchEvent(event);
float relX = event.getX() / getWidth(); float absX = event.getX();
float relY = event.getY() / getHeight(); float absY = event.getY();
Log.d(TAG, "onTouchEvent: absX=" + absX + ", absY=" + absY);
float relX = absX / getWidth();
float relY = absY / getHeight();
// 确保坐标在 [0, 1] 范围内 // 确保坐标在 [0, 1] 范围内
relX = Math.max(0, Math.min(1, relX)); relX = Math.max(0, Math.min(1, relX));
@@ -60,13 +66,13 @@ public class RemoteTouchView extends SurfaceView {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
lastTouchX = relX; lastTouchX = relX;
lastTouchY = relY; lastTouchY = relY;
lastAbsX = absX;
lastAbsY = absY;
swipeStartTime = System.currentTimeMillis(); swipeStartTime = System.currentTimeMillis();
return true; return true;
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
// 实时更新坐标(可选:用于拖拽效果) // 仅返回 true 以持续接收事件,不更新起始坐标,确保 onSwipe 计算的是从按下点开始的位移
lastTouchX = relX;
lastTouchY = relY;
return true; return true;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
@@ -77,11 +83,11 @@ public class RemoteTouchView extends SurfaceView {
if (duration < 200 && dx < 0.02 && dy < 0.02) { if (duration < 200 && dx < 0.02 && dy < 0.02) {
// 点击事件 // 点击事件
listener.onTouch(lastTouchX, lastTouchY); listener.onTouch(lastTouchX, lastTouchY);
Log.d(TAG, "Touch at: " + lastTouchX + ", " + lastTouchY); Log.d(TAG, "Touch at: " + lastTouchX + ", " + lastTouchY + " (abs: " + absX + ", " + absY + ")");
} else { } else {
// 滑动事件 // 滑动事件
listener.onSwipe(lastTouchX, lastTouchY, relX, relY, duration); listener.onSwipe(lastTouchX, lastTouchY, relX, relY, duration);
Log.d(TAG, "Swipe from (" + lastTouchX + "," + lastTouchY + ") to (" + relX + "," + relY + ")"); Log.d(TAG, "Swipe from (" + lastTouchX + "," + lastTouchY + ") [abs: " + lastAbsX + "," + lastAbsY + "] to (" + relX + "," + relY + ") [abs: " + absX + "," + absY + "]");
} }
return true; return true;
} }