private boolean touchDown(int x, int y) {
if (DEBUG) {
Log.d(LOG_TAG, "touchDown (" + x + ", " + y + ")");
}
mDownTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(
mDownTime, mDownTime, MotionEvent.ACTION_DOWN, x, y, 1);
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
return injectEventSync(event);
}
private boolean touchUp(int x, int y) {
if (DEBUG) {
Log.d(LOG_TAG, "touchUp (" + x + ", " + y + ")");
}
final long eventTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(
mDownTime, eventTime, MotionEvent.ACTION_UP, x, y, 1);
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
mDownTime = 0;
return injectEventSync(event);
}
private boolean touchMove(int x, int y) {
if (DEBUG) {
Log.d(LOG_TAG, "touchMove (" + x + ", " + y + ")");
}
final long eventTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(
mDownTime, eventTime, MotionEvent.ACTION_MOVE, x, y, 1);
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
return injectEventSync(event);
}
private boolean injectEventSync(InputEvent event, boolean sync) {
final int mode = (sync) ? InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH
: InputManager.INJECT_INPUT_EVENT_MODE_ASYNC;
InputManager inputManager = (InputManager) mContext.getSystemService(Context.INPUT_SERVICE);
return inputManager.injectInputEvent(event,mode);
}
Android模拟触摸事件
©著作权归作者所有:来自狐狸之家原创作品,如需转载,请注明出处,否则将追究法律责任