Friday, February 7, 2014

2.3 added a new overloaded MotionEvent.obtain (), how to use this API simulate two zoom operation


            
public static MotionEvent obtain (long downTime, long eventTime, int action, int pointers, int [] pointerIds, PointerCoords [] pointerCoords, int metaState, float xPrecision, float yPrecision, int deviceId, int edgeFlags, int source, int flags)

Since: API Level 9
Create a new MotionEvent, filling in all of the basic values ​​that define the motion.
Parameters

downTime The time (in ms) when the user originally pressed down to start a stream of position events. This must be obtained from uptimeMillis ().
eventTime The the time (in ms) when this specific event was generated. This must be obtained from uptimeMillis ().
action The kind of action being performed, such as ACTION_DOWN.
pointers The number of points that will be in this event.
pointerIds An array of pointers values ​​providing an identifier for each pointer.
pointerCoords An array of pointers values ​​providing a MotionEvent.PointerCoords coordinate object for each pointer.
metaState The state of any meta / modifier keys that were in effect when the event was generated.
xPrecision The precision of the X coordinate being reported.
yPrecision The precision of the Y coordinate being reported.
. deviceId The id for the device that this event came from An id of zero indicates that the event didn't come from a physical device; other numbers are arbitrary and you shouldn't depend on the values ​​
.edgeFlags A bitfield indicating which edges, if any, were touched by this MotionEvent.
source The source of this event.
flags The motion event flags.

2.3 above is a new API to add, how to use it and Instrumentation analog touch points, reaching zoom function?

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

}

This method attempts to write the above, and finally with
 twoPointTouch (240.0f, 400.0f, 240.0f, 20.0f, 241.0f, 401.0f, 241.0f, 600.0f, 40); 
test it, the result is to scroll effect. Test scenario is WebView, 2.3 simulator.

Some people have a way to make it work properly.
Reply:
Everyone says multitouch unit testing can not be used to test the standard Android framework Instrumentation. I have not tried, so always think of, did not find out who measure success.
Reply:
http://stackoverflow.com/questions/3637044/generating-multitouch-motionevents-for-testing
Looks like it was a success here, but did not paste code
Reply:
Landlord this problem you solve it?
It needs your conclusions ah
First Thank you!
Reply:
No results, Results posted
Reply:
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);

The ACTION is set incorrectly

1 Press → MotionEvent.ACTION_DOWN
2 Press → MotionEvent.ACTION_POINTER_DOWN + (motionEvent2Index << MotionEvent.ACTION_POINTER_INDEX_SHIFT)
Mobile → MotionEvent.MOVE
2 left → MotionEvent.ACTION_POINTER_UP + (motionEvent2Index << MotionEvent.ACTION_POINTER_INDEX_SHIFT)
1 left → MotionEvent.ACTION_UP

Which motionEventXIndex is PointerPropertiesArray, PointerCoords the Index of the MotionEvent
Reply:
Landlord to solve the problem yet, it helps!

No comments:

Post a Comment