下面列出了android.view.MotionEvent#setSource ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void injectTouchEventRelease(int x, int y)
{
MotionEvent me = MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis()+10,
MotionEvent.ACTION_UP,
x,
y,
0
);
if(Integer.valueOf(android.os.Build.VERSION.SDK_INT) < android.os.Build.VERSION_CODES.JELLY_BEAN)
;
else
me.setSource(InputDevice.SOURCE_TOUCHSCREEN);
injectEvent(me, INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT);
me.recycle();
}
public void injectTouchEventDown(int x, int y)
{
MotionEvent me = MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis()+10,
MotionEvent.ACTION_DOWN,
x,
y,
0
);
if(Integer.valueOf(android.os.Build.VERSION.SDK_INT) < android.os.Build.VERSION_CODES.JELLY_BEAN)
;
else
me.setSource(InputDevice.SOURCE_TOUCHSCREEN);
injectEvent(me, INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT);
me.recycle();
}
public boolean sendMotionEvent(int inputSource, int action, float x, float y) {
if (mInputManager == null || mInjectInputEventMethod == null) {
return false;
}
try {
long when = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(when, when, action, x, y, 1.0f, 1.0f,
0, 1.0f, 1.0f, 0, 0);
event.setSource(inputSource);
mInjectInputEventMethod.invoke(mInputManager, event, 0);
event.recycle();
return true;
} catch (Exception e) {
HJLog.e(e);
}
return false;
}
@Override
public void onEyeHitIn(MDHitEvent hitEvent) {
super.onEyeHitIn(hitEvent);
MDHitPoint point = hitEvent.getHitPoint();
if (point == null || mAttachedView == null) {
return;
}
int action = mTouchStatus == TouchStatus.NOP ? MotionEvent.ACTION_HOVER_ENTER : MotionEvent.ACTION_HOVER_MOVE;
float x = mAttachedView.getLeft() + mAttachedView.getWidth() * point.getU();
float y = mAttachedView.getTop() + mAttachedView.getHeight() * point.getV();
MotionEvent motionEvent = MotionEvent.obtain(hitEvent.getTimestamp(), System.currentTimeMillis(), action, x, y, 0);
motionEvent.setSource(InputDevice.SOURCE_CLASS_POINTER);
mAttachedView.dispatchGenericMotionEvent(motionEvent);
motionEvent.recycle();
mTouchStatus = TouchStatus.DOWN;
invalidate();
}
protected void sendCancelEvent(MotionEvent event) {
if (mHasSendCancelEvent || (event == null && mLastMoveEvent == null)) {
return;
}
if (sDebug) {
Log.d(TAG, "sendCancelEvent()");
}
final MotionEvent last = event == null ? mLastMoveEvent : event;
final long now = SystemClock.uptimeMillis();
final MotionEvent ev =
MotionEvent.obtain(
now, now, MotionEvent.ACTION_CANCEL, last.getX(), last.getY(), 0);
ev.setSource(InputDevice.SOURCE_TOUCHSCREEN);
mHasSendCancelEvent = true;
mHasSendDownEvent = false;
super.dispatchTouchEvent(ev);
ev.recycle();
}
/**
* Dispatch a pointer event. Finished at some point after the recipient has
* returned from its event processing, though it may <em>not</em> have
* completely finished reacting from the event -- for example, if it needs
* to update its display as a result, it may still be in the process of
* doing that.
*
* @param event A motion event describing the pointer action. (As noted in
* {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
* {@link SystemClock#uptimeMillis()} as the timebase.
*/
public void sendPointerSync(MotionEvent event) {
validateNotAppThread();
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
}
try {
WindowManagerGlobal.getWindowManagerService().injectInputAfterTransactionsApplied(event,
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
} catch (RemoteException e) {
}
}
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);
}
public boolean injectInputEvent(int action, float x, float y, int metaState) {
MotionEvent e = MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
action, x, y, metaState);
e.setSource(InputDevice.SOURCE_TOUCHSCREEN);
boolean b = uiAutomation.injectInputEvent(e, true);
e.recycle();
return b;
}
/**
* Resets gesture handlers state; called on didStartLoading().
* Note that this does NOT clear the pending motion events queue;
* it gets cleared in hasTouchEventHandlers() called from WebKit
* FrameLoader::transitionToCommitted iff the page ever had touch handlers.
*/
void resetGestureHandlers() {
MotionEvent me = obtainActionCancelMotionEvent();
me.setSource(InputDevice.SOURCE_CLASS_POINTER);
mGestureDetector.onTouchEvent(me);
mZoomManager.processTouchEvent(me);
me.recycle();
mLongPressDetector.cancelLongPress();
}
private boolean touchDown(int x, int y) {
mDownTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(mDownTime, mDownTime,
MotionEvent.ACTION_DOWN, x, y, 1.0f, 1.0f, 0, 1.0f, 1.0f,
eventId, 0);
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
return injectEventSync(event);
}
private boolean touchMove(int x, int y) {
final long eventTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(mDownTime, eventTime,
MotionEvent.ACTION_MOVE, x, y, 1.0f, 1.0f, 0, 1.0f, 1.0f,
eventId, 0);
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
return injectEventSync(event);
}
@Override
public void onEyeHitOut(long timestamp) {
super.onEyeHitOut(timestamp);
if (mTouchStatus == TouchStatus.DOWN){
MotionEvent motionEvent = MotionEvent.obtain(timestamp, System.currentTimeMillis(), MotionEvent.ACTION_HOVER_EXIT, 0, 0, 0);
motionEvent.setSource(InputDevice.SOURCE_CLASS_POINTER);
mAttachedView.dispatchGenericMotionEvent(motionEvent);
motionEvent.recycle();
}
mTouchStatus = TouchStatus.NOP;
invalidate();
}
@Override
public void onEyeHitOut(long timestamp) {
super.onEyeHitOut(timestamp);
if (mTouchStatus == TouchStatus.DOWN){
MotionEvent motionEvent = MotionEvent.obtain(timestamp, System.currentTimeMillis(), MotionEvent.ACTION_HOVER_EXIT, 0, 0, 0);
motionEvent.setSource(InputDevice.SOURCE_CLASS_POINTER);
mAttachedView.dispatchGenericMotionEvent(motionEvent);
motionEvent.recycle();
}
mTouchStatus = TouchStatus.NOP;
invalidate();
}
/**
* Dispatch a pointer event. Finished at some point after the recipient has
* returned from its event processing, though it may <em>not</em> have
* completely finished reacting from the event -- for example, if it needs
* to update its display as a result, it may still be in the process of
* doing that.
*
* @param event A motion event describing the pointer action. (As noted in
* {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
* {@link SystemClock#uptimeMillis()} as the timebase.
*/
public void sendPointerSync(MotionEvent event) {
validateNotAppThread();
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
}
InputManager.getInstance().injectInputEvent(event,
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
/**
* Dispatch a trackball event. Finished at some point after the recipient has
* returned from its event processing, though it may <em>not</em> have
* completely finished reacting from the event -- for example, if it needs
* to update its display as a result, it may still be in the process of
* doing that.
*
* @param event A motion event describing the trackball action. (As noted in
* {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
* {@link SystemClock#uptimeMillis()} as the timebase.
*/
public void sendTrackballEventSync(MotionEvent event) {
validateNotAppThread();
if ((event.getSource() & InputDevice.SOURCE_CLASS_TRACKBALL) == 0) {
event.setSource(InputDevice.SOURCE_TRACKBALL);
}
InputManager.getInstance().injectInputEvent(event,
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
/**
* Dispatch a pointer event. Finished at some point after the recipient has
* returned from its event processing, though it may <em>not</em> have
* completely finished reacting from the event -- for example, if it needs
* to update its display as a result, it may still be in the process of
* doing that.
*
* @param event A motion event describing the pointer action. (As noted in
* {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
* {@link SystemClock#uptimeMillis()} as the timebase.
*/
public void sendPointerSync(MotionEvent event) {
validateNotAppThread();
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
}
InputManager.getInstance().injectInputEvent(event,
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
/**
* Dispatch a trackball event. Finished at some point after the recipient has
* returned from its event processing, though it may <em>not</em> have
* completely finished reacting from the event -- for example, if it needs
* to update its display as a result, it may still be in the process of
* doing that.
*
* @param event A motion event describing the trackball action. (As noted in
* {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
* {@link SystemClock#uptimeMillis()} as the timebase.
*/
public void sendTrackballEventSync(MotionEvent event) {
validateNotAppThread();
if ((event.getSource() & InputDevice.SOURCE_CLASS_TRACKBALL) == 0) {
event.setSource(InputDevice.SOURCE_TRACKBALL);
}
InputManager.getInstance().injectInputEvent(event,
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
/**
* Dispatch a trackball event. Finished at some point after the recipient has
* returned from its event processing, though it may <em>not</em> have
* completely finished reacting from the event -- for example, if it needs
* to update its display as a result, it may still be in the process of
* doing that.
*
* @param event A motion event describing the trackball action. (As noted in
* {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
* {@link SystemClock#uptimeMillis()} as the timebase.
*/
public void sendTrackballEventSync(MotionEvent event) {
validateNotAppThread();
if ((event.getSource() & InputDevice.SOURCE_CLASS_TRACKBALL) == 0) {
event.setSource(InputDevice.SOURCE_TRACKBALL);
}
InputManager.getInstance().injectInputEvent(event,
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
/**
* Dispatch a pointer event. Finished at some point after the recipient has
* returned from its event processing, though it may <em>not</em> have
* completely finished reacting from the event -- for example, if it needs
* to update its display as a result, it may still be in the process of
* doing that.
*
* @param event A motion event describing the pointer action. (As noted in
* {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
* {@link SystemClock#uptimeMillis()} as the timebase.
*/
public void sendPointerSync(MotionEvent event) {
validateNotAppThread();
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
}
InputManager.getInstance().injectInputEvent(event,
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
/**
* Dispatch a pointer event. Finished at some point after the recipient has
* returned from its event processing, though it may <em>not</em> have
* completely finished reacting from the event -- for example, if it needs
* to update its display as a result, it may still be in the process of
* doing that.
*
* @param event A motion event describing the pointer action. (As noted in
* {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
* {@link SystemClock#uptimeMillis()} as the timebase.
*/
public void sendPointerSync(MotionEvent event) {
validateNotAppThread();
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
}
InputManager.getInstance().injectInputEvent(event,
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}