robotium拖动view到另一个view

2018-09-28  本文已影响0人  Ming_a221
/**
     * 拖拽View1到View2上
     * @param view1Id View1 id
     * @param view2
     */
    public void dragView1ToView2(String view1Id, View view2) {
        int[] location1 = new int[2];
        int[] location2 = new int[2];
        View view1 = (View) getView(view1Id);
        view1.getLocationOnScreen(location1);
        view2.getLocationOnScreen(location2);
        float xStart = location1[0];
        float yStart = location1[1];
        float xStop = location2[0];
        float yStop = location2[1];
        long downTime = SystemClock.uptimeMillis();
        long eventTime1 = SystemClock.uptimeMillis();
        MotionEvent event1 = MotionEvent.obtain(downTime, eventTime1, MotionEvent.ACTION_DOWN, xStart+10f, yStart+10f, 0);
        long eventTime2 = SystemClock.uptimeMillis() + 1000;
        MotionEvent event2 = MotionEvent.obtain(downTime, eventTime2, MotionEvent.ACTION_MOVE, xStop+10f, yStop+50f, 0);
        MotionEvent event3 = MotionEvent.obtain(downTime, eventTime2, MotionEvent.ACTION_UP, xStop+10f, yStop+50f, 0);
        event1.setSource(InputDevice.SOURCE_TOUCHSCREEN);
        event2.setSource(InputDevice.SOURCE_TOUCHSCREEN);
        event3.setSource(InputDevice.SOURCE_TOUCHSCREEN);
        instrumentation.getUiAutomation().injectInputEvent(event1, true);
        instrumentation.getUiAutomation().injectInputEvent(event2, true);
        instrumentation.getUiAutomation().injectInputEvent(event3, true);
    }
上一篇 下一篇

猜你喜欢

热点阅读