feat: 优化不跟手的问题
This commit is contained in:
@@ -111,12 +111,12 @@ public class MainActivity extends AppCompatActivity {
|
||||
touchOverlay.setTouchEventListener(new RemoteTouchView.TouchEventListener() {
|
||||
@Override
|
||||
public void onTouch(float relX, float relY) {
|
||||
sendTouchCommand(relX, relY);
|
||||
// 已通过 onMotionEvent 处理
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSwipe(float relX1, float relY1, float relX2, float relY2, long duration) {
|
||||
sendSwipeCommand(relX1, relY1, relX2, relY2, duration);
|
||||
// 已通过 onMotionEvent 处理
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -126,7 +126,13 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
public void onLongPress(float relX, float relY) {
|
||||
sendLongPressCommand(relX, relY);
|
||||
// 如果需要高层级的长按,可以保留,但 raw 模式下远程系统会自动识别
|
||||
// sendLongPressCommand(relX, relY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMotionEvent(int action, float relX, float relY) {
|
||||
sendMotionEventCommand(action, relX, relY);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -361,6 +367,17 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
}
|
||||
|
||||
private void sendMotionEventCommand(int action, float relX, float relY) {
|
||||
if (webRtcClient != null) {
|
||||
JsonObject cmd = new JsonObject();
|
||||
cmd.addProperty("action", "MOTION_EVENT");
|
||||
cmd.addProperty("motionAction", action);
|
||||
cmd.addProperty("x", relX);
|
||||
cmd.addProperty("y", relY);
|
||||
webRtcClient.sendControlCommand(cmd.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private void disconnect() {
|
||||
if (webRtcClient != null) {
|
||||
webRtcClient.close();
|
||||
|
||||
@@ -25,8 +25,6 @@ public class RemoteTouchView extends SurfaceView {
|
||||
|
||||
private float lastTouchX;
|
||||
private float lastTouchY;
|
||||
private float lastAbsX;
|
||||
private float lastAbsY;
|
||||
private long swipeStartTime;
|
||||
private boolean isLongPressed = false;
|
||||
private GestureDetector gestureDetector;
|
||||
@@ -36,6 +34,7 @@ public class RemoteTouchView extends SurfaceView {
|
||||
void onSwipe(float relX1, float relY1, float relX2, float relY2, long duration);
|
||||
void onKeyEvent(int keyCode, int action);
|
||||
void onLongPress(float relX, float relY);
|
||||
void onMotionEvent(int action, float relX, float relY);
|
||||
}
|
||||
|
||||
public RemoteTouchView(Context context) {
|
||||
@@ -101,7 +100,6 @@ public class RemoteTouchView extends SurfaceView {
|
||||
|
||||
float absX = event.getX();
|
||||
float absY = event.getY();
|
||||
Log.d(TAG, "onTouchEvent: absX=" + absX + ", absY=" + absY);
|
||||
|
||||
float relX = absX / getWidth();
|
||||
float relY = absY / getHeight();
|
||||
@@ -110,18 +108,20 @@ public class RemoteTouchView extends SurfaceView {
|
||||
relX = Math.max(0, Math.min(1, relX));
|
||||
relY = Math.max(0, Math.min(1, relY));
|
||||
|
||||
switch (event.getActionMasked()) {
|
||||
int action = event.getActionMasked();
|
||||
|
||||
// 发送原始 MotionEvent 实现实时跟手
|
||||
listener.onMotionEvent(action, relX, relY);
|
||||
|
||||
switch (action) {
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
isLongPressed = false;
|
||||
lastTouchX = relX;
|
||||
lastTouchY = relY;
|
||||
lastAbsX = absX;
|
||||
lastAbsY = absY;
|
||||
swipeStartTime = System.currentTimeMillis();
|
||||
return true;
|
||||
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
// 仅返回 true 以持续接收事件,不更新起始坐标,确保 onSwipe 计算的是从按下点开始的位移
|
||||
return true;
|
||||
|
||||
case MotionEvent.ACTION_UP:
|
||||
@@ -132,14 +132,11 @@ public class RemoteTouchView extends SurfaceView {
|
||||
float dx = Math.abs(relX - lastTouchX);
|
||||
float dy = Math.abs(relY - lastTouchY);
|
||||
|
||||
// 保留旧的回调以防万一,但 MainActivity 现在应该忽略它们
|
||||
if (duration < 200 && dx < 0.02 && dy < 0.02) {
|
||||
// 点击事件
|
||||
listener.onTouch(lastTouchX, lastTouchY);
|
||||
Log.d(TAG, "Touch at: " + lastTouchX + ", " + lastTouchY + " (abs: " + absX + ", " + absY + ")");
|
||||
} else {
|
||||
// 滑动事件
|
||||
listener.onSwipe(lastTouchX, lastTouchY, relX, relY, duration);
|
||||
Log.d(TAG, "Swipe from (" + lastTouchX + "," + lastTouchY + ") [abs: " + lastAbsX + "," + lastAbsY + "] to (" + relX + "," + relY + ") [abs: " + absX + "," + absY + "]");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -173,4 +170,13 @@ public class RemoteTouchView extends SurfaceView {
|
||||
cmd.addProperty("y", relY);
|
||||
return cmd.toString();
|
||||
}
|
||||
|
||||
public String createMotionEventCommand(int action, float relX, float relY) {
|
||||
JsonObject cmd = new JsonObject();
|
||||
cmd.addProperty("action", "MOTION_EVENT");
|
||||
cmd.addProperty("motionAction", action);
|
||||
cmd.addProperty("x", relX);
|
||||
cmd.addProperty("y", relY);
|
||||
return cmd.toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user