Skip to content

Commit

Permalink
Merge pull request #365 from caofengbin/feature/perfect_sas_swipe
Browse files Browse the repository at this point in the history
feat:基于SonicAPK的滑动,实现线性滑动的效果,避免过渡太生硬
  • Loading branch information
ZhouYixun authored Jun 21, 2023
2 parents 6cacc32 + 83a2ba1 commit f14bd37
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;

@Slf4j
public class AndroidTouchHandler {
private static final Map<String, OutputStream> outputMap = new ConcurrentHashMap<>();
private static final Map<String, Thread> touchMap = new ConcurrentHashMap<>();
private static final Map<String, TouchMode> touchModeMap = new ConcurrentHashMap<>();
private static final Map<String, int[]> sizeMap = new ConcurrentHashMap<>();
private static final int SWIPE_DURATION = 500;

public enum TouchMode {
SONIC_APK,
Expand Down Expand Up @@ -116,19 +118,47 @@ 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<Integer, Integer, Integer> transitionX = (start, end) ->
(int) (start + (end - start) * timeProgress);
BiFunction<Integer, Integer, Integer> 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) {
e.printStackTrace();
}
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.junit.Test;

import java.util.function.BiFunction;

public class AndroidTouchHandlerTest {

private int width = 720;
Expand All @@ -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<Integer, Integer, Integer> transitionX = (start, end) ->
(int) (start + (end - start) * timeProgress);
BiFunction<Integer, Integer, Integer> 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) {

}
}
}
}

0 comments on commit f14bd37

Please sign in to comment.