下面列出了android.view.MotionEvent#PointerCoords ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
Device(final int aDevice) {
mDevice = aDevice;
mProperties = new MotionEvent.PointerProperties[1];
mProperties[0] = new MotionEvent.PointerProperties();
mProperties[0].id = 0;
mProperties[0].toolType = MotionEvent.TOOL_TYPE_FINGER;
mCoords = new MotionEvent.PointerCoords[1];
mCoords[0] = new MotionEvent.PointerCoords();
mMouseOutCoords = new MotionEvent.PointerCoords[1];
for (MotionEvent.PointerCoords[] coords : Arrays.asList(mCoords, mMouseOutCoords)) {
coords[0] = new MotionEvent.PointerCoords();
coords[0].toolMajor = 2;
coords[0].toolMinor = 2;
coords[0].touchMajor = 2;
coords[0].touchMinor = 2;
}
mMouseOutCoords[0].x = -10;
mMouseOutCoords[0].y = -10;
}
private void dispatchEvent(
final int action,
final long start,
final long when,
final int pointerCount,
final MotionEvent.PointerProperties[] pointerProps,
final MotionEvent.PointerCoords[] pointerCoords) {
getRootView().post(
new Runnable() {
@Override
public void run() {
MotionEvent event =
MotionEvent.obtain(start, when, action, pointerCount, pointerProps, pointerCoords, 0, 0, 1.0f, 1.0f, 0, 0, 0, 0);
getRootView().dispatchTouchEvent(event);
event.recycle();
}
});
getInstrumentation().waitForIdleSync();
}
public static MotionEvent obtainMotionEvent(
long downTime,
long eventTime,
int action,
int id1,
float x1,
float y1,
int id2,
float x2,
float y2) {
int[] ids = new int[] {id1, id2};
MotionEvent.PointerCoords[] coords =
new MotionEvent.PointerCoords[] {createCoords(x1, y1), createCoords(x2, y2)};
MotionEvent.PointerProperties[] properties = {createProperties(id1), createProperties(id2)};
return MotionEvent.obtain(
downTime, eventTime, action, 2, properties, coords, 0, 0, 1.0f, 1.0f, 0, 0, 0, 0);
}
private static MotionEvent downPressICS(
long downTime, float[] coordinates, float[] precision, int inputDevice, int buttonState) {
MotionEvent.PointerCoords[] pointerCoords = {new MotionEvent.PointerCoords()};
MotionEvent.PointerProperties[] pointerProperties = getPointerProperties(inputDevice);
pointerCoords[0].clear();
pointerCoords[0].x = coordinates[0];
pointerCoords[0].y = coordinates[1];
pointerCoords[0].pressure = 0;
pointerCoords[0].size = 1;
return MotionEvent.obtain(
downTime,
SystemClock.uptimeMillis(),
MotionEvent.ACTION_DOWN,
1, // pointerCount
pointerProperties,
pointerCoords,
0, // metaState
buttonState,
precision[0],
precision[1],
0, // deviceId
0, // edgeFlags
inputDevice,
0); // flags
}
private MotionEvent transformEvent(MotionEvent e, int headerPosition) {
if (headerPosition == MATCHED_STICKIED_HEADER) {
return e;
}
long downTime = e.getDownTime();
long eventTime = e.getEventTime();
int action = e.getAction();
int pointerCount = e.getPointerCount();
int[] pointerIds = getPointerIds(e);
MotionEvent.PointerCoords[] pointerCoords = getPointerCoords(e);
int metaState = e.getMetaState();
float xPrecision = e.getXPrecision();
float yPrecision = e.getYPrecision();
int deviceId = e.getDeviceId();
int edgeFlags = e.getEdgeFlags();
int source = e.getSource();
int flags = e.getFlags();
View headerHolder = getChildAt(headerPosition);
for (int i = 0; i < pointerCount;i++) {
pointerCoords[i].y -= headerHolder.getTop();
}
MotionEvent n = MotionEvent.obtain(downTime, eventTime, action,
pointerCount, pointerIds, pointerCoords, metaState, xPrecision,
yPrecision, deviceId, edgeFlags, source, flags);
return n;
}
private boolean handleMotionEvent(MotionEvent event) {
if (!mAttached) {
mQueuedEvents.add(new Pair(EVENT_SOURCE_MOTION, event));
return false;
}
if (event.getPointerCount() <= 0) {
return false;
}
final int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) {
mLastDownTime = event.getDownTime();
} else if (mLastDownTime != event.getDownTime()) {
return false;
}
final MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords();
event.getPointerCoords(0, coords);
if (action == MotionEvent.ACTION_UP) {
mSession.click((int) coords.x, (int) coords.y);
}
return true;
}
private boolean handleScrollEvent(MotionEvent event) {
if (!mAttached) {
mQueuedEvents.add(new Pair(EVENT_SOURCE_SCROLL, event));
return false;
}
if (event.getPointerCount() <= 0) {
return false;
}
final MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords();
event.getPointerCoords(0, coords);
mSession.getSurfaceBounds(mTempRect);
final float x = coords.x - mTempRect.left;
final float y = coords.y - mTempRect.top;
final float hScroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL) * mPointerScrollFactor;
final float vScroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL) * mPointerScrollFactor;
if (!mIsScrolling) {
mSession.scrollStart((int) hScroll, (int) vScroll, (int) x, (int) y);
} else {
mSession.scroll((int) hScroll, (int) vScroll, (int) x, (int) y);
}
mIsScrolling = true;
return true;
}
private static MotionEvent.PointerCoords[] getPointerCoords(MotionEvent e) {
int n = e.getPointerCount();
MotionEvent.PointerCoords[] r = new MotionEvent.PointerCoords[n];
for (int i = 0; i < n; i++) {
r[i] = new MotionEvent.PointerCoords();
e.getPointerCoords(i, r[i]);
}
return r;
}
private MotionEvent transformEvent(MotionEvent e, int headerPosition) {
if (headerPosition == MATCHED_STICKIED_HEADER) {
return e;
}
long downTime = e.getDownTime();
long eventTime = e.getEventTime();
int action = e.getAction();
int pointerCount = e.getPointerCount();
int[] pointerIds = getPointerIds(e);
MotionEvent.PointerCoords[] pointerCoords = getPointerCoords(e);
int metaState = e.getMetaState();
float xPrecision = e.getXPrecision();
float yPrecision = e.getYPrecision();
int deviceId = e.getDeviceId();
int edgeFlags = e.getEdgeFlags();
int source = e.getSource();
int flags = e.getFlags();
View headerHolder = getChildAt(headerPosition);
for (int i = 0; i < pointerCount;i++) {
pointerCoords[i].y -= headerHolder.getTop();
}
MotionEvent n = MotionEvent.obtain(downTime, eventTime, action,
pointerCount, pointerIds, pointerCoords, metaState, xPrecision,
yPrecision, deviceId, edgeFlags, source, flags);
return n;
}
private MotionEvent transformEvent(MotionEvent e, int headerPosition) {
if (headerPosition == MATCHED_STICKIED_HEADER) {
return e;
}
long downTime = e.getDownTime();
long eventTime = e.getEventTime();
int action = e.getAction();
int pointerCount = e.getPointerCount();
int[] pointerIds = getPointerIds(e);
MotionEvent.PointerCoords[] pointerCoords = getPointerCoords(e);
int metaState = e.getMetaState();
float xPrecision = e.getXPrecision();
float yPrecision = e.getYPrecision();
int deviceId = e.getDeviceId();
int edgeFlags = e.getEdgeFlags();
int source = e.getSource();
int flags = e.getFlags();
View headerHolder = getChildAt(headerPosition);
for (int i = 0; i < pointerCount;i++) {
pointerCoords[i].y -= headerHolder.getTop();
}
MotionEvent n = MotionEvent.obtain(downTime, eventTime, action,
pointerCount, pointerIds, pointerCoords, metaState, xPrecision,
yPrecision, deviceId, edgeFlags, source, flags);
return n;
}
public MotionInputEventParams(long startDelta, int actionCode, MotionEvent.PointerCoords coordinates,
int button, MotionEvent.PointerProperties properties) {
super();
this.startDelta = startDelta;
this.actionCode = actionCode;
this.coordinates = coordinates;
this.button = button;
this.properties = properties;
}
private static MotionEvent.PointerCoords[] filterPointerCoordinates(
final List<MotionInputEventParams> motionEventsParams, final boolean shouldHovering) {
final List<MotionEvent.PointerCoords> result = new ArrayList<>();
for (final MotionInputEventParams eventParams : motionEventsParams) {
if (shouldHovering && HOVERING_ACTIONS.contains(eventParams.actionCode) &&
eventParams.properties.toolType == MotionEvent.TOOL_TYPE_MOUSE) {
result.add(eventParams.coordinates);
} else if (!shouldHovering && !HOVERING_ACTIONS.contains(eventParams.actionCode)) {
result.add(eventParams.coordinates);
}
}
return result.toArray(new MotionEvent.PointerCoords[0]);
}
public static MotionEvent obtainMotionEvent(
long downTime, long eventTime, int action, int id1, float x1, float y1) {
int[] ids = new int[] {id1};
MotionEvent.PointerCoords[] coords = new MotionEvent.PointerCoords[] {createCoords(x1, y1)};
MotionEvent.PointerProperties[] properties = {createProperties(id1)};
return MotionEvent.obtain(
downTime, eventTime, action, 1, properties, coords, 0, 0, 1.0f, 1.0f, 0, 0, 0, 0);
}
private static MotionEvent.PointerCoords[] getPointerCoords(MotionEvent e) {
int n = e.getPointerCount();
MotionEvent.PointerCoords[] r = new MotionEvent.PointerCoords[n];
for (int i = 0; i < n; i++) {
r[i] = new MotionEvent.PointerCoords();
e.getPointerCoords(i, r[i]);
}
return r;
}
private static MotionEvent.PointerCoords[] getPointerCoords(MotionEvent e) {
int n = e.getPointerCount();
MotionEvent.PointerCoords[] r = new MotionEvent.PointerCoords[n];
for (int i = 0; i < n; i++) {
r[i] = new MotionEvent.PointerCoords();
e.getPointerCoords(i, r[i]);
}
return r;
}
List<MotionEvent> convertTouchEvent(OnTouchEvent onTouchEvent){
List<MotionEvent> motionEventList = new ArrayList<MotionEvent>();
List<TouchEvent> touchEventList = onTouchEvent.getEvent();
if (touchEventList == null || touchEventList.size() == 0) return null;
TouchType touchType = onTouchEvent.getType();
if (touchType == null) { return null; }
if(sdlMotionEvent == null) {
if (touchType == TouchType.BEGIN) {
sdlMotionEvent = new SdlMotionEvent();
} else{
return null;
}
}
SdlMotionEvent.Pointer pointer;
MotionEvent motionEvent;
for (TouchEvent touchEvent : touchEventList) {
if (touchEvent == null || touchEvent.getId() == null) {
continue;
}
List<TouchCoord> touchCoordList = touchEvent.getTouchCoordinates();
if (touchCoordList == null || touchCoordList.size() == 0) {
continue;
}
TouchCoord touchCoord = touchCoordList.get(touchCoordList.size() - 1);
if (touchCoord == null) {
continue;
}
int motionEventAction = sdlMotionEvent.getMotionEventAction(touchType, touchEvent);
long downTime = sdlMotionEvent.downTime;
long eventTime = sdlMotionEvent.eventTime;
pointer = sdlMotionEvent.getPointerById(touchEvent.getId());
if (pointer != null) {
pointer.setCoords(touchCoord.getX() * touchScalar[0], touchCoord.getY() * touchScalar[1]);
}
MotionEvent.PointerProperties[] pointerProperties = new MotionEvent.PointerProperties[sdlMotionEvent.pointers.size()];
MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[sdlMotionEvent.pointers.size()];
for (int i = 0; i < sdlMotionEvent.pointers.size(); i++) {
pointerProperties[i] = new MotionEvent.PointerProperties();
pointerProperties[i].id = sdlMotionEvent.getPointerByIndex(i).id;
pointerProperties[i].toolType = MotionEvent.TOOL_TYPE_FINGER;
pointerCoords[i] = new MotionEvent.PointerCoords();
pointerCoords[i].x = sdlMotionEvent.getPointerByIndex(i).x;
pointerCoords[i].y = sdlMotionEvent.getPointerByIndex(i).y;
pointerCoords[i].orientation = 0;
pointerCoords[i].pressure = 1.0f;
pointerCoords[i].size = 1;
}
motionEvent = MotionEvent.obtain(downTime, eventTime, motionEventAction,
sdlMotionEvent.pointers.size(), pointerProperties, pointerCoords, 0, 0, 1,
1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);
motionEventList.add(motionEvent);
if(motionEventAction == MotionEvent.ACTION_UP || motionEventAction == MotionEvent.ACTION_CANCEL){
//If the motion event should be finished we should clear our reference
sdlMotionEvent.pointers.clear();
sdlMotionEvent = null;
break;
} else if((motionEventAction & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_UP){
sdlMotionEvent.removePointerById(touchEvent.getId());
}
}
return motionEventList;
}
private MotionEvent.PointerCoords createPointerCoords(float x, float y) {
MotionEvent.PointerCoords pointerCoords = new MotionEvent.PointerCoords();
pointerCoords.x = x;
pointerCoords.y = y;
return pointerCoords;
}
private void injectTouchEvent(int buttonId, int event, int x, int y)
{
final View view = obtainTargetView();
if (view == null) return;
final Activity activity = obtainActivity();
if (activity == null) return;
int viewLocation[] = new int[2];
view.getLocationOnScreen(viewLocation);
MotionEvent.PointerProperties pp = new MotionEvent.PointerProperties();
pp.toolType = MotionEvent.TOOL_TYPE_FINGER;
pp.id = 0;
MotionEvent.PointerProperties[] pps = new MotionEvent.PointerProperties[]{pp};
MotionEvent.PointerCoords pc = new MotionEvent.PointerCoords();
pc.size = 1;
pc.pressure = 1;
pc.x = x - viewLocation[0];
pc.y = y - viewLocation[1];
MotionEvent.PointerCoords[] pcs = new MotionEvent.PointerCoords[]{pc};
long t = SystemClock.uptimeMillis();
final MotionEvent e = MotionEvent.obtain(
t, // long downTime
t + 100, // long eventTime
event, // int action
pps.length, // int pointerCount
pps, // MotionEvent.PointerProperties[] pointerProperties
pcs, // MotionEvent.PointerCoords[] pointerCoords
0, // int metaState
0, // int buttonState
1, // float xPrecision
1, // float yPrecision
1, // int deviceId
0, // int edgeFlags
InputDevice.SOURCE_TOUCHSCREEN, //int source
0 // int flags
);
activity.runOnUiThread(new Runnable() {
public void run() {
view.dispatchTouchEvent(e);
}
});
}
private static MotionEvent.PointerCoords extractCoordinates(final String actionId,
final List<W3CGestureModel> gestures,
final int itemIdx) {
if (itemIdx < 0) {
throw new ActionsParseException(String.format(
"The first item of action '%s' cannot define HOVER move, " +
"because its start coordinates are not set", actionId));
}
final W3CGestureModel gesture = gestures.get(itemIdx);
if (!ACTION_ITEM_TYPE_POINTER_MOVE.equals(gesture.type)) {
if (itemIdx > 0) {
return extractCoordinates(actionId, gestures, itemIdx - 1);
}
throw new ActionsParseException(String.format(
"Action item '%s' of action '%s' should be preceded with at least one item " +
"with coordinates", gesture, actionId));
}
Object origin = gesture.origin == null ? ACTION_ITEM_ORIGIN_VIEWPORT : gesture.origin;
final MotionEvent.PointerCoords result = new MotionEvent.PointerCoords();
result.size = gesture.size == null ? 1 : gesture.size.floatValue();
result.pressure = gesture.pressure == null ? 1 : gesture.pressure.floatValue();
if (origin instanceof String) {
if (origin.equals(ACTION_ITEM_ORIGIN_VIEWPORT)) {
if (gesture.x == null || gesture.y == null) {
throw new ActionsParseException(String.format(
"Both coordinates must be be set for action item '%s' of action '%s'",
gesture, actionId));
}
result.x = gesture.x.floatValue();
result.y = gesture.y.floatValue();
return result;
} else if (origin.equals(ACTION_ITEM_ORIGIN_POINTER)) {
if (itemIdx > 0) {
final MotionEvent.PointerCoords recentCoords = extractCoordinates(actionId, gestures, itemIdx - 1);
result.x = recentCoords.x;
result.y = recentCoords.y;
if (gesture.x != null) {
result.x += gesture.x.floatValue();
}
if (gesture.y != null) {
result.y += gesture.y.floatValue();
}
return result;
}
throw new ActionsParseException(String.format(
"Action item '%s' of action '%s' should be preceded with at least one item " +
"containing absolute coordinates", gesture, actionId));
}
}
return extractElementCoordinates(actionId, gesture, origin);
}
public static MotionEvent.PointerCoords createCoords(float x, float y) {
MotionEvent.PointerCoords pointerCoords = new MotionEvent.PointerCoords();
pointerCoords.x = x;
pointerCoords.y = y;
return pointerCoords;
}