Here are the examples of the java api class android.view.MotionEvent.PointerCoords taken from open source projects.
1. TouchEventSynthesizer#setPointer()
Project: chromium_webview
File: TouchEventSynthesizer.java
File: TouchEventSynthesizer.java
@CalledByNative void setPointer(int index, int x, int y, int id) { assert (0 <= index && index < MAX_NUM_POINTERS); // Convert coordinates from density independent pixels to density dependent pixels. float scaleFactor = mContentViewCore.getRenderCoordinates().getDeviceScaleFactor(); PointerCoords coords = new PointerCoords(); coords.x = scaleFactor * x; coords.y = scaleFactor * y; coords.pressure = 1.0f; mPointerCoords[index] = coords; PointerProperties properties = new PointerProperties(); properties.id = id; mPointerProperties[index] = properties; }
2. Zoomer#generateZoomGesture()
Project: robotium
File: Zoomer.java
File: Zoomer.java
public void generateZoomGesture(PointF startPoint1, PointF startPoint2, PointF endPoint1, PointF endPoint2) { long downTime = SystemClock.uptimeMillis(); long eventTime = SystemClock.uptimeMillis(); float startX1 = startPoint1.x; float startY1 = startPoint1.y; float startX2 = startPoint2.x; float startY2 = startPoint2.y; float endX1 = endPoint1.x; float endY1 = endPoint1.y; float endX2 = endPoint2.x; float endY2 = endPoint2.y; //pointer 1 float x1 = startX1; float y1 = startY1; //pointer 2 float x2 = startX2; float y2 = startY2; PointerCoords[] pointerCoords = new PointerCoords[2]; PointerCoords pc1 = new PointerCoords(); PointerCoords pc2 = new PointerCoords(); pc1.x = x1; pc1.y = y1; pc1.pressure = 1; pc1.size = 1; pc2.x = x2; pc2.y = y2; pc2.pressure = 1; pc2.size = 1; pointerCoords[0] = pc1; pointerCoords[1] = pc2; PointerProperties[] pointerProperties = new PointerProperties[2]; PointerProperties pp1 = new PointerProperties(); PointerProperties pp2 = new PointerProperties(); pp1.id = 0; pp1.toolType = MotionEvent.TOOL_TYPE_FINGER; pp2.id = 1; pp2.toolType = MotionEvent.TOOL_TYPE_FINGER; pointerProperties[0] = pp1; pointerProperties[1] = pp2; MotionEvent event; // send the initial touches event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 1, pointerProperties, pointerCoords, // metaState, buttonState 0, // metaState, buttonState 0, // x precision 1, // y precision 1, 0, 0, 0, // deviceId, edgeFlags, source, flags 0); _instrument.sendPointerSync(event); event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_POINTER_DOWN + (pp2.id << MotionEvent.ACTION_POINTER_INDEX_SHIFT), 2, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, 0, 0); _instrument.sendPointerSync(event); int numMoves = GESTURE_DURATION_MS / EVENT_TIME_INTERVAL_MS; float stepX1 = (endX1 - startX1) / numMoves; float stepY1 = (endY1 - startY1) / numMoves; float stepX2 = (endX2 - startX2) / numMoves; float stepY2 = (endY2 - startY2) / numMoves; // send the zoom for (int i = 0; i < numMoves; i++) { eventTime += EVENT_TIME_INTERVAL_MS; pointerCoords[0].x += stepX1; pointerCoords[0].y += stepY1; pointerCoords[1].x += stepX2; pointerCoords[1].y += stepY2; event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, 2, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, 0, 0); _instrument.sendPointerSync(event); } }
3. Tapper#generateTapGesture()
Project: robotium
File: Tapper.java
File: Tapper.java
public void generateTapGesture(int numTaps, PointF... points) { MotionEvent event; long downTime = SystemClock.uptimeMillis(); long eventTime = SystemClock.uptimeMillis(); // pointer 1 float x1 = points[0].x; float y1 = points[0].y; float x2 = 0; float y2 = 0; if (points.length == 2) { // pointer 2 x2 = points[1].x; y2 = points[1].y; } PointerCoords[] pointerCoords = new PointerCoords[points.length]; PointerCoords pc1 = new PointerCoords(); pc1.x = x1; pc1.y = y1; pc1.pressure = 1; pc1.size = 1; pointerCoords[0] = pc1; PointerCoords pc2 = new PointerCoords(); if (points.length == 2) { pc2.x = x2; pc2.y = y2; pc2.pressure = 1; pc2.size = 1; pointerCoords[1] = pc2; } PointerProperties[] pointerProperties = new PointerProperties[points.length]; PointerProperties pp1 = new PointerProperties(); pp1.id = 0; pp1.toolType = MotionEvent.TOOL_TYPE_FINGER; pointerProperties[0] = pp1; PointerProperties pp2 = new PointerProperties(); if (points.length == 2) { pp2.id = 1; pp2.toolType = MotionEvent.TOOL_TYPE_FINGER; pointerProperties[1] = pp2; } int i = 0; while (i != numTaps) { event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, points.length, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0); _instrument.sendPointerSync(event); if (points.length == 2) { event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_POINTER_DOWN + (pp2.id << MotionEvent.ACTION_POINTER_INDEX_SHIFT), points.length, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0); _instrument.sendPointerSync(event); eventTime += EVENT_TIME_INTERVAL_MS; event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_POINTER_UP + (pp2.id << MotionEvent.ACTION_POINTER_INDEX_SHIFT), points.length, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0); _instrument.sendPointerSync(event); } eventTime += EVENT_TIME_INTERVAL_MS; event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, points.length, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0); _instrument.sendPointerSync(event); i++; } }
4. Swiper#generateSwipeGesture()
Project: robotium
File: Swiper.java
File: Swiper.java
public void generateSwipeGesture(PointF startPoint1, PointF startPoint2, PointF endPoint1, PointF endPoint2) { long downTime = SystemClock.uptimeMillis(); long eventTime = SystemClock.uptimeMillis(); float startX1 = startPoint1.x; float startY1 = startPoint1.y; float startX2 = startPoint2.x; float startY2 = startPoint2.y; float endX1 = endPoint1.x; float endY1 = endPoint1.y; float endX2 = endPoint2.x; float endY2 = endPoint2.y; // pointer 1 float x1 = startX1; float y1 = startY1; // pointer 2 float x2 = startX2; float y2 = startY2; PointerCoords[] pointerCoords = new PointerCoords[2]; PointerCoords pc1 = new PointerCoords(); PointerCoords pc2 = new PointerCoords(); pc1.x = x1; pc1.y = y1; pc1.pressure = 1; pc1.size = 1; pc2.x = x2; pc2.y = y2; pc2.pressure = 1; pc2.size = 1; pointerCoords[0] = pc1; pointerCoords[1] = pc2; PointerProperties[] pointerProperties = new PointerProperties[2]; PointerProperties pp1 = new PointerProperties(); PointerProperties pp2 = new PointerProperties(); pp1.id = 0; pp1.toolType = MotionEvent.TOOL_TYPE_FINGER; pp2.id = 1; pp2.toolType = MotionEvent.TOOL_TYPE_FINGER; pointerProperties[0] = pp1; pointerProperties[1] = pp2; MotionEvent event; // send the initial touches event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 1, pointerProperties, pointerCoords, // metaState, buttonState 0, // metaState, buttonState 0, // x precision 1, // y precision 1, 0, 0, 0, // deviceId, edgeFlags, source, flags 0); _instrument.sendPointerSync(event); event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_POINTER_DOWN + (pp2.id << MotionEvent.ACTION_POINTER_INDEX_SHIFT), 2, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, 0, 0); _instrument.sendPointerSync(event); int numMoves = GESTURE_DURATION_MS / EVENT_TIME_INTERVAL_MS; float stepX1 = (endX1 - startX1) / numMoves; float stepY1 = (endY1 - startY1) / numMoves; float stepX2 = (endX2 - startX2) / numMoves; float stepY2 = (endY2 - startY2) / numMoves; // send the zoom for (int i = 0; i < numMoves; i++) { eventTime += EVENT_TIME_INTERVAL_MS; pointerCoords[0].x += stepX1; pointerCoords[0].y += stepY1; pointerCoords[1].x += stepX2; pointerCoords[1].y += stepY2; event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, 2, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, 0, 0); _instrument.sendPointerSync(event); } }
5. Rotator#generateRotateGesture()
Project: robotium
File: Rotator.java
File: Rotator.java
public void generateRotateGesture(int size, PointF center1, PointF center2) { double incrementFactor = 0; float startX1 = center1.x; float startY1 = center1.y; float startX2 = center2.x; float startY2 = center2.y; long downTime = SystemClock.uptimeMillis(); long eventTime = SystemClock.uptimeMillis(); // pointer 1 float x1 = startX1; float y1 = startY1; // pointer 2 float x2 = startX2; float y2 = startY2; PointerCoords[] pointerCoords = new PointerCoords[2]; PointerCoords pc1 = new PointerCoords(); PointerCoords pc2 = new PointerCoords(); pc1.x = x1; pc1.y = y1; pc1.pressure = 1; pc1.size = 1; pc2.x = x2; pc2.y = y2; pc2.pressure = 1; pc2.size = 1; pointerCoords[0] = pc1; pointerCoords[1] = pc2; PointerProperties[] pointerProperties = new PointerProperties[2]; PointerProperties pp1 = new PointerProperties(); PointerProperties pp2 = new PointerProperties(); pp1.id = 0; pp1.toolType = MotionEvent.TOOL_TYPE_FINGER; pp2.id = 1; pp2.toolType = MotionEvent.TOOL_TYPE_FINGER; pointerProperties[0] = pp1; pointerProperties[1] = pp2; MotionEvent event; // send the initial touches event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 1, pointerProperties, pointerCoords, // metaState, buttonState 0, // metaState, buttonState 0, // x precision 1, // y precision 1, // deviceId, edgeFlags 0, // deviceId, edgeFlags 0, InputDevice.SOURCE_TOUCHSCREEN, // source, flags 0); _instrument.sendPointerSync(event); event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_POINTER_DOWN + (pp2.id << MotionEvent.ACTION_POINTER_INDEX_SHIFT), 2, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0); _instrument.sendPointerSync(event); switch(size) { case 0: { incrementFactor = 0.01; } break; case 1: { incrementFactor = 0.1; } break; } for (double i = 0; i < Math.PI; i += incrementFactor) { eventTime += EVENT_TIME_INTERVAL_MS; pointerCoords[0].x += Math.cos(i); pointerCoords[0].y += Math.sin(i); pointerCoords[1].x += Math.cos(i + Math.PI); pointerCoords[1].y += Math.sin(i + Math.PI); event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, 2, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0); _instrument.sendPointerSync(event); } // and remove them fingers from the screen eventTime += EVENT_TIME_INTERVAL_MS; event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_POINTER_UP + (pp2.id << MotionEvent.ACTION_POINTER_INDEX_SHIFT), 2, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0); _instrument.sendPointerSync(event); eventTime += EVENT_TIME_INTERVAL_MS; event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, 1, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0); _instrument.sendPointerSync(event); }
6. Illustrator#illustrate()
Project: robotium
File: Illustrator.java
File: Illustrator.java
public void illustrate(Illustration illustration) { if (illustration == null || illustration.getPoints().isEmpty()) { throw new IllegalArgumentException("Illustration must not be null and requires at least one point."); } MotionEvent event; int currentAction; long downTime = SystemClock.uptimeMillis(); long eventTime = SystemClock.uptimeMillis(); PointerCoords[] coords = new PointerCoords[1]; PointerCoords coord = new PointerCoords(); PointerProperties[] properties = new PointerProperties[1]; PointerProperties prop = new PointerProperties(); prop.id = 0; prop.toolType = illustration.getToolType(); properties[0] = prop; coords[0] = coord; ArrayList<PressurePoint> points = illustration.getPoints(); for (int i = 0; i < points.size(); i++) { PressurePoint currentPoint = points.get(i); coord.x = currentPoint.x; coord.y = currentPoint.y; coord.pressure = currentPoint.pressure; coord.size = 1; if (i == 0) { currentAction = MotionEvent.ACTION_DOWN; } else { currentAction = MotionEvent.ACTION_MOVE; } eventTime = SystemClock.uptimeMillis(); event = MotionEvent.obtain(downTime, eventTime, currentAction, 1, properties, coords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0); try { inst.sendPointerSync(event); } catch (SecurityException ignored) { } } currentAction = MotionEvent.ACTION_UP; coords[0] = coord; PressurePoint currentPoint = points.get(points.size() - 1); coord.x = currentPoint.x; coord.y = currentPoint.y; coord.pressure = currentPoint.pressure; coord.size = 1; eventTime = SystemClock.uptimeMillis(); event = MotionEvent.obtain(downTime, eventTime, currentAction, 1, properties, coords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0); try { inst.sendPointerSync(event); } catch (SecurityException ignored) { } }
7. AndroidTouchScreen#getPointerCoords()
Project: selendroid
File: AndroidTouchScreen.java
File: AndroidTouchScreen.java
private PointerCoords[] getPointerCoords() { PointerCoords[] pointerCoords = new PointerCoords[pointers.size()]; int i = 0; for (Pointer p : pointers) { pointerCoords[i++] = p.getCoords(); } return pointerCoords; }
8. LocalLib#sendMultiTouchMotionEvent()
Project: Cafe
File: LocalLib.java
File: LocalLib.java
/** * send a Multi-Touch Motion Event * * @param pointerNumber * the number of pointer * @param start * the start position e.g. new int[]{0,0,1,2}; means two pointers * start at {0,0} and {1,2} * @param end * the end position e.g. new int[]{100,110,200,220}; means two * pointers end at {100,110} and {200,220} * @param step * the move step * @param downDelay * the delay after down event was sent * @param moveDelay * the delay after each move event was sent * @param upDelay * the delay before sending up event */ @SuppressLint("Recycle") @SuppressWarnings("deprecation") public void sendMultiTouchMotionEvent(int pointerNumber, int[] start, int[] end, int step, int downDelay, int moveDelay, int upDelay) { double[] delta = new double[pointerNumber * 2]; int[] pointerIds = new int[pointerNumber]; PointerCoords[] pointerPositions = new PointerCoords[pointerNumber]; int temp = 0; for (int i = 0; i < pointerNumber; i++) { pointerPositions[i] = new PointerCoords(); pointerPositions[i].pressure = 1.0f; temp = i * 2; delta[temp] = (end[temp] - start[temp]) / (double) step; pointerPositions[i].x = start[temp]; temp++; delta[temp] = (end[temp] - start[temp]) / (double) step; pointerPositions[i].y = start[temp]; pointerIds[i] = i; } long myTime = SystemClock.uptimeMillis(); mInstrumentation.sendPointerSync(MotionEvent.obtain(myTime, myTime, MotionEvent.ACTION_DOWN, pointerNumber, pointerIds, pointerPositions, 0, 0.1f, 0.1f, 0, 0, 0, 0)); this.sleep(downDelay); for (int i = 0; i < step; i++) { for (int j = 0; j < pointerNumber; j++) { temp = j * 2; pointerPositions[j].x = (float) (start[temp] + delta[temp] * (i + 1)); temp++; pointerPositions[j].y = (float) (start[temp] + delta[temp] * (i + 1)); } myTime = SystemClock.uptimeMillis(); mInstrumentation.sendPointerSync(MotionEvent.obtain(myTime, myTime, MotionEvent.ACTION_MOVE, pointerNumber, pointerIds, pointerPositions, 0, 0.1f, 0.1f, 0, 0, 0, 0)); this.sleep(moveDelay); } this.sleep(upDelay); myTime = SystemClock.uptimeMillis(); mInstrumentation.sendPointerSync(MotionEvent.obtain(myTime, myTime, MotionEvent.ACTION_UP, pointerNumber, pointerIds, pointerPositions, 0, 0.1f, 0.1f, 0, 0, 0, 0)); }