只能在AndroidTest里运行
UiDevice myDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
try {
myDevice.findObject(new UiSelector().text("element text to be found")).pinchIn(50, 10); //to zoom in
myDevice.findObject(new UiSelector().text("element text to be found")).pinchOut(50, 20); //to zoom out
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
接下来,走到pinchIn里:
//UiObject.class
public boolean pinchIn(int percent, int steps) throws UiObjectNotFoundException {
percent = percent < 0 ? 0 : (percent > 100 ? 100 : percent);
float percentage = (float)percent / 100.0F;
AccessibilityNodeInfo node = this.findAccessibilityNodeInfo(this.mConfig.getWaitForSelectorTimeout());
if (node == null) {
throw new UiObjectNotFoundException(this.mUiSelector.toString());
} else {
Rect rect = this.getVisibleBounds(node);
if (rect.width() <= 40) {
throw new IllegalStateException("Object width is too small for operation");
} else {
Point startPoint1 = new Point(rect.centerX() - (int)((float)(rect.width() / 2) * percentage), rect.centerY());
Point startPoint2 = new Point(rect.centerX() + (int)((float)(rect.width() / 2) * percentage), rect.centerY());
Point endPoint1 = new Point(rect.centerX() - 20, rect.centerY());
Point endPoint2 = new Point(rect.centerX() + 20, rect.centerY());
return this.performTwoPointerGesture(startPoint1, startPoint2, endPoint1, endPoint2, steps);
}
}
}
public boolean performTwoPointerGesture(Point startPoint1, Point startPoint2, Point endPoint1, Point endPoint2, int steps) {
if (steps == 0) {
steps = 1;
}
float stepX1 = (float)((endPoint1.x - startPoint1.x) / steps);
float stepY1 = (float)((endPoint1.y - startPoint1.y) / steps);