您的位置首页百科知识

Android MotionEvent.obtain方法使用

Android MotionEvent.obtain方法使用

的有关信息介绍如下:

Android MotionEvent.obtain方法使用

今天小编和大家分享Android MotionEvent.obtain方法使用经验,希望对大家有所帮助。

android MotionEvent.obtain模拟事件,自动触发:

view.setOnTouchListener(new OnTouchListener()

{

public boolean onTouch(View v, MotionEvent event)

{

Toast toast = Toast.makeText(

getApplicationContext(),

"View touched",

Toast.LENGTH_LONG

);

toast.show();

return true;

}

});

// 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);

private void twoPointTouch(float xOneStart, float yOneStart, float xOneEnd, float yOneEnd,

float xTwoStart, float yTwoStart, float xTwoEnd, float yTwoEnd, int stepCount) {

long downTime = SystemClock.uptimeMillis();

long eventTime = SystemClock.uptimeMillis();

MotionEvent.PointerCoords pointerCoordsOneStart = new MotionEvent.PointerCoords();

pointerCoordsOneStart.x = xOneStart;

pointerCoordsOneStart.y = yOneStart;

MotionEvent.PointerCoords pointerCoordsTwoStart = new MotionEvent.PointerCoords();

pointerCoordsTwoStart.x = xTwoStart;

pointerCoordsTwoStart.y = yTwoStart;

MotionEvent.PointerCoords pointerCoordsOneEnd = new MotionEvent.PointerCoords();

pointerCoordsOneEnd.x = xOneEnd;

pointerCoordsOneEnd.y = yOneEnd;

MotionEvent.PointerCoords pointerCoordsTwoEnd = new MotionEvent.PointerCoords();

pointerCoordsTwoEnd.x = xTwoEnd;

pointerCoordsTwoEnd.y = yTwoEnd;

float xOneStep = (xOneEnd - xOneStart) / stepCount;

float yOneStep = (yOneEnd - yOneStart) / stepCount;

float xTwoStep = (xTwoEnd - xTwoStart) / stepCount;

float yTwoStep = (yTwoEnd - xTwoStart) / stepCount;

MotionEvent.PointerCoords pointerCoordsOneStep = new MotionEvent.PointerCoords();

pointerCoordsOneStep.x = xOneStart;

pointerCoordsOneStep.y = yOneStart;

MotionEvent.PointerCoords pointerCoordsTwoStep = new MotionEvent.PointerCoords();

pointerCoordsTwoStep.x = xTwoStart;

pointerCoordsTwoStep.y = yTwoStart;

MotionEvent event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,

2, new int[] {0, 1}, new MotionEvent.PointerCoords[] {pointerCoordsOneStart, pointerCoordsTwoStart}, 0, 0.0f,

0.0f, 0, 0, 0, 0);

Log.e(LOG_TAG, "this event has " + event.getPointerCount() + "action is " + event.getAction());

inst.sendPointerSync(event);

inst.waitForIdleSync();

for (int i = 0; i < stepCount; ++i) {

pointerCoordsOneStep.x += xOneStep;

pointerCoordsOneStep.y += yOneStep;

pointerCoordsTwoStep.x += xTwoStep;

pointerCoordsTwoStep.y += yTwoStep;

eventTime = SystemClock.uptimeMillis();

event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE,

2, new int[] {0, 1}, new MotionEvent.PointerCoords[] {pointerCoordsOneStep, pointerCoordsTwoStep}, 0, 0.0f,

0.0f, 0, 0, 0, 0);

Log.e(LOG_TAG, "pointerCoordsOneStep x is " + pointerCoordsOneStep.x);

Log.e(LOG_TAG, "pointerCoordsOneStep y is " + pointerCoordsOneStep.y);

Log.i(LOG_TAG, "pointerCoordsTwoStep x is " + pointerCoordsTwoStep.x);

Log.i(LOG_TAG, "pointerCoordsTwoStep y is " + pointerCoordsTwoStep.y);

inst.sendPointerSync(event);

inst.waitForIdleSync();

}

eventTime = SystemClock.uptimeMillis();

event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,

2, new int[] {0, 1}, new MotionEvent.PointerCoords[] {pointerCoordsOneEnd, pointerCoordsTwoEnd}, 0, 0.0f,

0.0f, 0, 0, 0, 0);

inst.sendPointerSync(event);

inst.waitForIdleSync();