如何以编程方式触发Android中的触摸事件?

我想触发像这样的触摸事件:

首先,手指触摸屏幕的(0.50%)并滑动到屏幕的(50%,50%),然后退出(将手指从屏幕上移开)

我发现了一些这样的事情:

MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, pressure, size, metaState, xPrecision, yPrecision, deviceId, edgeFlags); onTouchEvent(event); 

但是,如何模仿上述情况呢? 我是否需要创build2个事件? onTouchDown,onMove等….? 感谢您的帮助。

Solutions Collecting From Web of "如何以编程方式触发Android中的触摸事件?"

可能的重复: 如何在Android中模拟触摸事件?

 // Obtain MotionEvent object long downTime = SystemClock.uptimeMillis(); long eventTime = SystemClock.uptimeMillis() + 100; float x = 0.0f; float y = 0.0f; // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState() int metaState = 0; MotionEvent motionEvent = MotionEvent.obtain( downTime, eventTime, MotionEvent.ACTION_UP, x, y, metaState ); // Dispatch touch event to view view.dispatchTouchEvent(motionEvent);