如何使用android-Instrumentation拖动?

How to use android-Instrumentation to drag?

这是我的 function.It 可以工作但不能设置速度。不知道这样用对不对

那么,实现拖动的正确方法是什么?

public static void drag(final int start_x, final int start_y, final int end_x, final int end_y) {
    mInst = new Instrumentation();
    
    //key down
    mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, start_x, start_y, 0));
    //move
    mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, start_x, start_y, 0));
    mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, end_x, end_y, 0));
    //key up
    mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, end_x, end_y, 0));

}
public static void drag(float x1, float y1, float x2, float y2, float duration) {
        final int interval = 25;
        int steps = (int) (duration * 1000 / interval + 1);
        float dx = (x2 - x1) / steps;
        float dy = (y2 - y1) / steps;
        down(x1, y1);
        for (int step = 0; step < steps; step++) {
            SystemClock.sleep(interval);
            moveTo(x1 + step * dx, y1 + step * dy, 0);
        }
        SystemClock.sleep(interval);
        up(x2, y2);
    }


    private static void down(float x, float y) {
        if (Robot.mInst == null) {
            mInst = new Instrumentation();
        }
        mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, x, y, 0));
    }


    private static void up(float x, float y) {
        if (Robot.mInst == null) {
            mInst = new Instrumentation();
        }
        mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, x, y, 0));
    }


    private static void moveTo(float x, float y, int contactId) {
        if (Robot.mInst == null) {
            mInst = new Instrumentation();
        }
        mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, x, y, 0));
    }

有效!