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)); diff --git a/src/test/java/org/cloud/sonic/agent/tests/handlers/AndroidTouchHandlerTest.java b/src/test/java/org/cloud/sonic/agent/tests/handlers/AndroidTouchHandlerTest.java index 5c8ff0c2..d88d803f 100755 --- a/src/test/java/org/cloud/sonic/agent/tests/handlers/AndroidTouchHandlerTest.java +++ b/src/test/java/org/cloud/sonic/agent/tests/handlers/AndroidTouchHandlerTest.java @@ -2,6 +2,8 @@ import org.junit.Test; +import java.util.function.BiFunction; + public class AndroidTouchHandlerTest { private int width = 720; @@ -27,4 +29,39 @@ public void test() { System.out.println(re[0]); System.out.println(re[1]); } + + @Test + public void test_interpolator() { + int[] re1 = {200, 1200}; + int[] re2 = {200, 300}; + // 过渡总时间 + int duration = 500; + // 开始时间 + long startTime = System.currentTimeMillis(); + while (true) { + // 当前时间 + long currentTime = System.currentTimeMillis(); + float timeProgress = (currentTime - startTime) / (float) duration; + if (timeProgress >= 1.0f) { + // 已经过渡到结束值,停止过渡 + System.out.println("[" + re2[0] + "," + re2[1] + "]"); + break; + } + BiFunction transitionX = (start, end) -> + (int) (start + (end - start) * timeProgress); + BiFunction transitionY = (start, end) -> + (int) (start + (end - start) * timeProgress); // Y 坐标过渡函数 + + int currentX = transitionX.apply(re1[0], re2[0]); // 当前 X 坐标 + int currentY = transitionY.apply(re1[1], re2[1]); // 当前 Y 坐标 + // 使用当前坐标进行操作 + // ... + System.out.println("[" + currentX + "," + currentY + "]"); + try { + Thread.sleep(10); + } catch (InterruptedException e) { + + } + } + } }