You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android无障碍服务自动点击器点击位置偏移问题求助

解决Accessibility Service自动点击器点击View中心的问题

核心问题分析

你之前的点击位置落在右下角,大概率是直接使用了Rect.right和Rect.bottom作为点击坐标,或是计算中心时基准点错误。正确做法是基于View的屏幕边界矩形,精准计算中心坐标。

具体实现步骤

  1. 获取View的屏幕边界矩形
    通过AccessibilityNodeInfo.getBoundsInScreen()方法,获取目标View在屏幕上的绝对位置矩形:

    val targetNode: AccessibilityNodeInfo = clickView // 你的目标节点
    val screenRect = Rect()
    targetNode.getBoundsInScreen(screenRect)
    
  2. 计算中心坐标
    基于矩形的左、上边界加上宽高的一半,得到精确的中心坐标:

    val centerX = screenRect.left + screenRect.width() / 2
    val centerY = screenRect.top + screenRect.height() / 2
    

    用left + width/2而非right - width/2,可避免整数计算误差,结果完全一致。

  3. 执行精准点击
    使用AccessibilityService的手势API模拟点击(比直接调用performAction(AccessibilityNodeInfo.ACTION_CLICK)更可靠,尤其适配复杂布局场景):

    val gesture = GestureDescription.Builder().apply {
        val clickPath = Path()
        clickPath.moveTo(centerX.toFloat(), centerY.toFloat())
        // 添加点击手势:从中心位置触发,无延迟,持续100ms
        addStroke(GestureDescription.StrokeDescription(clickPath, 0, 100))
    }.build()
    
    dispatchGesture(gesture, object : AccessibilityService.GestureResultCallback() {
        override fun onCompleted(gestureDescription: GestureDescription?) {
            // 点击完成后的逻辑(可选)
        }
    
        override fun onCancelled(gestureDescription: GestureDescription?) {
            // 点击被取消的逻辑(可选)
        }
    }, null)
    

额外注意事项

  • 如果目标节点本身支持点击,也可先尝试targetNode.performAction(AccessibilityNodeInfo.ACTION_CLICK),但这种方式由系统自动处理点击位置,复杂布局下可能不如手势精准。
  • 确保AccessibilityService已正确获取权限,且能正常定位到目标AccessibilityNodeInfo。

内容的提问来源于stack exchange,提问作者Victor Ang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 17:50:33