帮我做一个关于手机自动滑动的脚本,可以自行修改方向和时间

HAMI_7296054880 · 12 小时前

回答

Hamibot AI
1
· 12 小时前

文档地址 https://docs.hamibot.com/

以下是一个可自定义滑动方向和持续时间的通用脚本。它支持上下、左右以及对角线方向的滑动,并通过参数控制滑动时长。

// 手机自动滑动脚本 - 支持方向和时间自定义

/**
 * 执行指定方向的滑动操作
 * @param {string} direction - 滑动方向: 'up', 'down', 'left', 'right'
 * @param {number} duration - 滑动持续时间(毫秒),默认 400ms
 */
function autoSwipe(direction, duration) {
    // 默认滑动时间
    if (typeof duration !== 'number' || isNaN(duration) || duration <= 0) {
        duration = 400;
    }

    // 获取屏幕尺寸
    var width = device.width;
    var height = device.height;

    // 定义起点和终点坐标
    var x1, y1, x2, y2;

    // 根据方向设置滑动路径
    switch (direction.toLowerCase()) {
        case 'up':   // 上滑:从下往上
            x1 = width / 2;
            y1 = height * 0.8;
            x2 = width / 2;
            y2 = height * 0.2;
            break;
        case 'down': // 下滑:从上往下
            x1 = width / 2;
            y1 = height * 0.2;
            x2 = width / 2;
            y2 = height * 0.8;
            break;
        case 'left': // 左滑:从右往左
            x1 = width * 0.8;
            y1 = height / 2;
            x2 = width * 0.2;
            y2 = height / 2;
            break;
        case 'right': // 右滑:从左往右
            x1 = width * 0.2;
            y1 = height / 2;
            x2 = width * 0.8;
            y2 = height / 2;
            break;
        default:
            throw new Error('不支持的方向,仅支持 up, down, left, right');
    }

    // 执行滑动动作
    swipe(x1, y1, x2, y2, duration);
}

// === 使用示例 ===

// 上滑,持续 500ms
autoSwipe('up', 500);

// 等待一段时间再执行下一个动作
sleep(1000);

// 下滑,使用默认时间(400ms)
autoSwipe('down');

sleep(1000);

// 右滑,持续 600ms
autoSwipe('right', 600);

功能说明:

  • autoSwipe(direction, duration) 是主函数。
  • direction 参数控制滑动方向,支持 'up', 'down', 'left', 'right'
  • duration 控制滑动过程所用时间(单位为毫秒),数值越大滑动越慢。
  • 脚本使用 swipe() 函数实现平滑滑动,兼容大多数安卓设备。

注意事项:

  • 确保 Hamibot 已获取无障碍服务权限。
  • 如果在某些应用中标准 swipe 失效,可以尝试改用 RootAutomator 或 gesture 方式模拟更真实的手势。
  • 坐标基于屏幕比例设定,适配不同分辨率设备。

你可以根据需要修改起始位置或添加更多复杂轨迹(如弧形滑动)。若需更高仿真度,建议结合贝塞尔曲线算法优化路径。

ℹ️ 本回答由 AI 生成,内容仅供参考