1

如何以编程方式模拟手势。

我得到了所有的分数,我希望我的应用程序能重复这些图纸。

public void onGestureStarted(GestureOverlayView gestureOverlayView, MotionEvent motionEvent) {
            Log.d("TAG", "Started gesture: \n" + motionEvent.getRawX() + "\n" +
                    motionEvent.getRawY() + "\n" + motionEvent.getEventTime());

            gestures.add(new GesturePoint(motionEvent.getRawX(), motionEvent.getRawY(), 100));
        }

        @Override
        public void onGesture(GestureOverlayView gestureOverlayView, MotionEvent motionEvent) {
            //To change body of implemented methods use File | Settings | File Templates.
            gestures.add(new GesturePoint(motionEvent.getRawX(), motionEvent.getRawY(), 100));
            Log.e("TAG", "gesture: \n" + motionEvent.getRawX() + "\n" +
                    motionEvent.getRawY() + "\n" + motionEvent.getEventTime());
        }

        @Override
        public void onGestureEnded(GestureOverlayView gestureOverlayView, MotionEvent motionEvent) {
            gestures.add(new GesturePoint(motionEvent.getRawX(), motionEvent.getRawY(), 100));

            Log.d("TAG", "Ended gesture: \n" + motionEvent.getRawX() + "\n" +
                    motionEvent.getRawY() + "\n" + motionEvent.getEventTime());

            g.addStroke(new GestureStroke(gestures));
            gestureOverlayView.setGesture(g);

有没有像 startDraw(Arraylist) 这样的方法?

我需要它来模拟用户交互。

4

1 回答 1

2

您可以使用MotionEvent类。简单使用:

MotionEvent emulateActionDown = MotionEvent.obtain
(
    // The time (in ms) when the user originally pressed down to start a stream of position events. 
    // This must be obtained from uptimeMillis().
    SystemClock.uptimeMillis(),
    // The the time (in ms) when this specific event was generated. 
    // This must be obtained from uptimeMillis().
    SystemClock.uptimeMillis()+20,
    // The kind of action being performed, such as ACTION_DOWN.
    MotionEvent.ACTION_DOWN, 
    // The X coordinate of this event.
    1, 
    // The Y coordinate of this event.
    1, 
    // The state of any meta / modifier keys that were in effect when the event was generated. 
    0
)

示例显示了一个简单的 ACTION_DOWN 模拟。然后,您可以将此事件传递给View 的onTouchEvent方法:

myView.onTouchEvent(emulateActionDown);

看看这里:MotionEvent -> 获取

于 2013-05-10T16:20:04.603 回答