Android UiAutomator2 pinch捏合缩放手势源码探究

只能在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);
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值