### minitouch 模块简介
`minitouch` 是一种用于向 Android 设备发送触摸事件的工具,通常被集成到自动化测试框架中。它能够提供高效的屏幕触控能力,并支持多点触控操作。相比传统的 `adb shell input tap` 命令,`minitouch` 提供更高的精度和更快的速度。
以下是关于 `minitouch` 的安装指南以及使用方法:
---
### 安装指南
#### 方法一:通过 ADB 推送二进制文件
1. 首先下载适合目标设备架构的 `minitouch` 二进制文件。可以从 GitHub 上获取预编译版本[^4]。
2. 将下载好的 `minitouch` 文件推送到设备上:
```bash
adb push minitouch /data/local/tmp/
```
3. 赋予可执行权限:
```bash
adb shell chmod 755 /data/local/tmp/minitouch
```
#### 方法二:借助第三方库自动部署
如果正在使用 `uiautomator2` 库,则可以通过其内置的功能来简化 `minitouch` 的配置过程。例如,在 Python 中运行以下命令即可初始化并启用 `minitouch` 功能:
```python
import uiautomator2 as u2
d = u2.connect()
d.mini_touch_enable() # 启用 mini touch 支持
```
此函数会在后台自动推送所需的二进制文件至设备,并启动服务[^4]。
---
### 使用说明
一旦成功安装并启用了 `minitouch`,就可以通过客户端程序与其交互以发送各种类型的触摸指令。下面是一些常见的 API 和示例代码片段。
#### 单击操作
单击指定位置 (x,y),持续时间为默认值(毫秒单位)。
```python
from uiautomator2 import DeviceMiniTouch
mt = DeviceMiniTouch(d)
mt.tap(x=100, y=200) # 在坐标(100, 200)处触发一次点击
```
#### 多指滑动
创建两个手指分别从起点移动到终点的过程演示如下所示:
```python
actions = [
{"start": (50, 50), "end": (150, 150)}, # 手指1路径定义
{"start": (200, 50), "end": (200, 250)} # 手指2路径定义
]
def multi_swipe(dev_mt, actions_list):
dev_mt.down(*actions_list[0]['start']) # 下压第一个手指
if len(actions_list)>1:
for i in range(1,len(actions_list)):
dev_mt.down(i,*actions_list[i]['start'])
steps_count = max(abs(a['end'][0]-a['start'][0])//10 , abs(a['end'][1]-a['start'][1])//10)+1
step_x=[int((action["end"][0]-action["start"][0])/steps_count)for action in actions_list]
step_y=[int((action["end"][1]-action["start"][1])/steps_count)for action in actions_list]
current_pos=[[action["start"][0],action["start"][1]]for action in actions_list ]
while any([abs(end[0]-cur[0])+abs(end[1]-cur[1])>0 for cur,end in zip(current_pos,[act["end"]for act in actions_list ])]):
for idx,(dx,dy)in enumerate(zip(step_x,step_y)):
new_x,new_y=current_pos[idx][0]+dx,current_pos[idx][1]+dy
within_bounds=new_x>=0 and new_y >=0and new_x<=dev_mt.max_x and new_y <=dev_mt.max_y
if not within_bounds or(new_x==current_pos[idx][0]and new_y ==current_pos[idx][1]):
continue
dev_mt.move(idx,new_x,new_y )
current_pos[idx]=[new_x,new_y ]
multi_swipe(mt, actions)
[d.up(i) for i,_ in enumerate(actions)]
```
以上脚本实现了双指或多指的同时拖拽动作,其中每一步都计算了精确的位置变化量,并逐步更新直到达到目的地为止[^3]。
---
### 注意事项
- **兼容性问题**:部分旧版Android系统可能无法正常工作,请确保目标设备满足最低要求。
- **性能优化**:对于复杂手势序列建议批量提交而非逐帧处理,这样能显著减少延迟提升流畅度。
- **异常捕获**:实际应用过程中需考虑网络中断等情况下的错误恢复机制设计。
---