简体   繁体   English

MonkeyRunner:确定 MonkeyDevice 触摸命令坐标的简单方法?

[英]MonkeyRunner: easy way to determine coordinates for MonkeyDevice touch command?

I've started creating a MonkeyRunner script.我已经开始创建一个 MonkeyRunner 脚本。 This is going ok, but whenever I add a MonkeyDevice::touch command, I have to determine the input coordinates by trial-and-error.这没问题,但是每当我添加 MonkeyDevice::touch 命令时,我都必须通过反复试验来确定输入坐标。 Basically I guess at the coordinates I want to touch and see if those coordinates result in the button touch I'm trying to test.基本上我猜我想触摸的坐标,看看这些坐标是否会导致我试图测试的按钮触摸。 That works, but it's a slow process.这是有效的,但这是一个缓慢的过程。 Is there anyway to determine the coordinates of UI controls, perhaps from the layout XML files?无论如何确定UI控件的坐标,也许是从布局XML文件?

I found how to do it.我找到了怎么做。 Use the Pixel Perfect view within Eclipse to determine the x & y coordinates of the UI element.使用 Eclipse 中的 Pixel Perfect 视图来确定 UI 元素的 x 和 y 坐标。 Here's a quick overview: 1) Eclipse must be running 2) Your Android device must be connected (either the real device via ADB, or the emulator) 3) Run the hierarchy viewer (in /tools) 4) Select "Inspect screenshot" The Pixel Perfect view will launch automatically.这是一个快速概述:1) Eclipse 必须正在运行 2) 您的 Android 设备必须已连接(通过 ADB 的真实设备或模拟器) 3) 运行层次结构查看器(在 /tools 中) 4) 选择“检查屏幕截图” Pixel Perfect 视图将自动启动。 Just place the cross-hairs on the UI element.只需将十字准线放在 UI 元素上。 The x and y coordinates, along with the RGB values, are displayed below. x 和 y 坐标以及 RGB 值显示在下方。

Here's the URL that got me started: http://developer.android.com/guide/developing/debugging/debugging-ui.html#pixelperfect这是让我开始的 URL: http : //developer.android.com/guide/developing/debugging/debugging-ui.html#pixelperfect

这篇文章( monkeyrunner:与视图交互)可以让您了解如何使用AndroidViewClient获取视图的坐标。

Most of the Android versions you can enable pointer location in Settings->Developer Options .大多数 Android 版本您可以在Settings->Developer Options启用指针位置。 Once you enable it, it is easy to find out the (x,y) coordinates.启用它后,很容易找出(x,y)坐标。

您还可以使用 AndroidSDK>tools 文件夹中的 HierarchyViewer 工具截取当前屏幕的屏幕截图并逐个像素地检查该图像以获取精确坐标。

For devices older than Android 4.0, see the paragraph following this one.对于早于 Android 4.0 的设备,请参阅此后的段落。 Android 4.0 and later include Settings->Developer options->Pointer location toggle which toggles a transparent ribbon across the top of the device screen with coordinates, velocities and touch-pressure readings including swipe tracks and x/y crosshairs for the current touch location. Android 4.0 及更高版本包括设置-> 开发人员选项-> 指针位置切换,可在设备屏幕顶部切换透明色带,其中包含坐标、速度和触摸压力读数,包括当前触摸位置的滑动轨迹和 x/y 十字准线。 This is a lot easier than using alternatives such as Monkey Recorder and other means.这比使用 Monkey Recorder 和其他方法等替代方法要容易得多。 In Android 4.2 and later, Developer options is hidden from the Settings menu and must be enabled by going to Settings->About tablet and tapping on Build number seven times.在 Android 4.2 及更高版本中,开发者选项在设置菜单中隐藏,必须通过转到设置->关于平板电脑并点击内部版本号七次来启用。 One can only presume that Android hid Developer options because of the increasingly user-experience-affecting options it contains and the number of consumer calls/complaints to device makers from people who played with it or whose children played with it.人们只能假设 Android 隐藏了开发者选项,因为它包含越来越多的影响用户体验的选项,以及来自玩过它的人或其孩子玩过它的人对设备制造商的消费者电话/投诉的数量。

In older versions which may not include a Pointer location toggle, there is an app on the Play Store (aka Android Market), Developers Tools.在可能不包含指针位置切换的旧版本中,Play 商店(又名 Android 市场)上有一个应用程序,即开发者工具。 See link here: https://play.google.com/store/apps/details?id=com.ggb.development It will show up with a gear icon and the caption Dev Tools on a device and provide similar functionality which Dev Tools in an AVD (Android Virtual Device) has.请参阅此处的链接: https : //play.google.com/store/apps/details?id=com.ggb.development它将在设备上显示一个齿轮图标和标题 Dev Tools,并提供与 Dev Tools 类似的功能在 AVD(Android 虚拟设备)中有。 That includes Dev Tools ->Development settings>No Pointer Location/Pointer Location radio button toggle.这包括开发工具 -> 开发设置 > 无指针位置/指针位置单选按钮切换。 Setting that toggle to Pointer Location provides the exact same functionality built-in with Android 4.0 and later.将该切换设置为指针位置提供了与 Android 4.0 及更高版本内置的完全相同的功能。 The same application also has a more limited pointer setting at Dev Tools->Pointer Location which limits pointer location to only a blank screen.同一个应用程序在 Dev Tools->Pointer Location 处也有更有限的指针设置,将指针位置限制为仅空白屏幕。

Enjoy!享受!

Create a xyz.py file with below code and connect the device and run it in terminal like monkeyrunner xyz.py, then you will get your device in pc, then u click on any button in the recorder it will give you the coordinates, after that explore it to any file and you can use the coordinates.用下面的代码创建一个 xyz.py 文件并连接设备并在终端中运行它,如monkeyrunner xyz.py,然后你将把你的设备放在电脑上,然后你点击记录器中的任何按钮它会给你坐标,之后探索它到任何文件,你可以使用坐标。

from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder
device = mr.waitForConnection()
recorder.start(device)

I found an adb approach.我找到了一种 adb 方法。 Use adb shell getevent -l to get a list of events, grep for ABS_MT_POSITION (gets the line with touch events in hex) and finally use awk to get the relevant hex values, strip them of zeros and convert hex to decimal that monkey runner uses.使用adb shell getevent -l获取事件列表,grep for ABS_MT_POSITION (以十六进制获取触摸事件的行),最后使用 awk 获取相关的十六进制值, ABS_MT_POSITION零并将十六进制转换为猴子跑步者使用的十进制. This is all done with the following:这一切都是通过以下方式完成的:

adb shell getevent -l | grep ABS_MT_POSITION --line-buffered | awk '{a = substr($0,54,8); sub(/^0+/, "", a); b = sprintf("0x%s",a); printf("%d\n",strtonum(b))}'

This continuously prints the x and y coordinates in the terminal only when you press on the device.仅当您按下设备时,这才会在终端中连续打印 x 和 y 坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM