在Android中模拟手势

时间:2013-05-09 12:56:42

标签: android gestures

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

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

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)这样的方法?

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

1 个答案:

答案 0 :(得分: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 -> obtain