From 83a2ba1dddf24fea962f0d08d93f989731067aac Mon Sep 17 00:00:00 2001 From: caofengbin <1050430934@qq.com> Date: Tue, 20 Jun 2023 20:03:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E4=BA=8ESONIC=5FAPK=E7=9A=84=E6=BB=91?= =?UTF-8?q?=E5=8A=A8=E6=93=8D=E4=BD=9C=EF=BC=8C=E5=A2=9E=E5=8A=A0=E7=BA=BF?= =?UTF-8?q?=E6=80=A7=E6=BB=91=E5=8A=A8=E7=9A=84=E6=95=88=E6=9E=9C=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E8=BF=87=E6=B8=A1=E5=A4=AA=E7=94=9F=E7=A1=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tests/handlers/AndroidTouchHandler.java | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidTouchHandler.java b/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidTouchHandler.java index d12cf02b..cb30347c 100755 --- a/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidTouchHandler.java +++ b/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidTouchHandler.java @@ -37,6 +37,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; +import java.util.function.BiFunction; @Slf4j public class AndroidTouchHandler { @@ -44,6 +45,7 @@ public class AndroidTouchHandler { private static final Map touchMap = new ConcurrentHashMap<>(); private static final Map touchModeMap = new ConcurrentHashMap<>(); private static final Map sizeMap = new ConcurrentHashMap<>(); + private static final int SWIPE_DURATION = 500; public enum TouchMode { SONIC_APK, @@ -116,7 +118,34 @@ public static void swipe(IDevice iDevice, int x1, int y1, int x2, int y2) throws } catch (InterruptedException e) { e.printStackTrace(); } - writeToOutputStream(iDevice, String.format("move %d %d\n", re2[0], re2[1])); + // 默认500毫秒完成滑动操作 + int duration = SWIPE_DURATION; + long startTime = System.currentTimeMillis(); + while (true) { + // 当前时间 + long currentTime = System.currentTimeMillis(); + // 计算时间进度 + float timeProgress = (currentTime - startTime) / (float) duration; + if (timeProgress >= 1.0f) { + // 已经过渡到结束值,停止过渡 + writeToOutputStream(iDevice, String.format("move %d %d\n", re2[0], re2[1])); + break; + } + BiFunction transitionX = (start, end) -> + (int) (start + (end - start) * timeProgress); + BiFunction transitionY = (start, end) -> + (int) (start + (end - start) * timeProgress); + + int currentX = transitionX.apply(re1[0], re2[0]); + int currentY = transitionY.apply(re1[1], re2[1]); + // 使用当前坐标进行操作 + writeToOutputStream(iDevice, String.format("move %d %d\n", currentX, currentY)); + try { + Thread.sleep(5); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } try { Thread.sleep(200); } catch (InterruptedException e) { @@ -124,11 +153,12 @@ public static void swipe(IDevice iDevice, int x1, int y1, int x2, int y2) throws } writeToOutputStream(iDevice, "up\n"); } - case ADB -> AndroidDeviceBridgeTool.executeCommand(iDevice, String.format("input swipe %d %d %d %d %d", x1, y1, x2, y2, 300)); + case ADB -> AndroidDeviceBridgeTool.executeCommand(iDevice, String.format("input swipe %d %d %d %d %d", + x1, y1, x2, y2, SWIPE_DURATION)); case APPIUM_UIAUTOMATOR2_SERVER -> { AndroidStepHandler curStepHandler = HandlerMap.getAndroidMap().get(iDevice.getSerialNumber()); if (curStepHandler != null && curStepHandler.getAndroidDriver() != null) { - curStepHandler.getAndroidDriver().swipe(x1, y1, x2, y2); + curStepHandler.getAndroidDriver().swipe(x1, y1, x2, y2, SWIPE_DURATION); } } default -> throw new IllegalStateException("Unexpected value: " + getTouchMode(iDevice));