下面列出了android.view.MotionEvent#PointerProperties ( ) 实例代码,或者点击链接到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 static MotionEvent upPressICS(MotionEvent downEvent, float[] coordinates) {
MotionEvent.PointerCoords[] pointerCoords = {new MotionEvent.PointerCoords()};
MotionEvent.PointerProperties[] pointerProperties = getPointerProperties(downEvent.getSource());
pointerCoords[0].clear();
pointerCoords[0].x = coordinates[0];
pointerCoords[0].y = coordinates[1];
pointerCoords[0].pressure = 0;
pointerCoords[0].size = 1;
return MotionEvent.obtain(
downEvent.getDownTime(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_UP,
1, // pointerCount
pointerProperties,
pointerCoords,
0, // metaState
downEvent.getButtonState(),
downEvent.getXPrecision(),
downEvent.getYPrecision(),
0, // deviceId
0, // edgeFlags
downEvent.getSource(),
0); // flags
}
private static MotionEvent.PointerProperties[] getPointerProperties(int inputDevice) {
MotionEvent.PointerProperties[] pointerProperties = {new MotionEvent.PointerProperties()};
pointerProperties[0].clear();
pointerProperties[0].id = 0;
switch (inputDevice) {
case InputDevice.SOURCE_MOUSE:
pointerProperties[0].toolType = MotionEvent.TOOL_TYPE_MOUSE;
break;
case InputDevice.SOURCE_STYLUS:
pointerProperties[0].toolType = MotionEvent.TOOL_TYPE_STYLUS;
break;
case InputDevice.SOURCE_TOUCHSCREEN:
pointerProperties[0].toolType = MotionEvent.TOOL_TYPE_FINGER;
break;
default:
pointerProperties[0].toolType = MotionEvent.TOOL_TYPE_UNKNOWN;
break;
}
return pointerProperties;
}
private void buildMoveEvent(MotionEvent event){
MotionEvent.PointerProperties[] pointerProperties = new MotionEvent.PointerProperties[]{new MotionEvent.PointerProperties()};
event.getPointerProperties(0, pointerProperties[0]);
MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[]{new MotionEvent.PointerCoords()};
event.getPointerCoords(0, pointerCoords[0]);
pointerProperties[0].toolType = MotionEvent.TOOL_TYPE_MOUSE;
pointerCoords[0].x -= this.getX();
pointerCoords[0].y -= this.getY();
long touchTime = SystemClock.uptimeMillis();
this.onTouchEvent(MotionEvent.obtain(touchTime, touchTime, MotionEvent.ACTION_MOVE, 1,
pointerProperties, pointerCoords, 0, 0, 0f, 0f,
InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC, 0, InputDevice.SOURCE_TOUCHSCREEN, 0));
}
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.PointerProperties[] filterPointerProperties(
final List<MotionInputEventParams> motionEventsParams, final boolean shouldHovering) {
final List<MotionEvent.PointerProperties> 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.properties);
} else if (!shouldHovering && !HOVERING_ACTIONS.contains(eventParams.actionCode)) {
result.add(eventParams.properties);
}
}
return result.toArray(new MotionEvent.PointerProperties[0]);
}
@Test
public void verifySortingOrderInNormalizedEvents() {
MotionEvent.PointerProperties down1Props = new MotionEvent.PointerProperties();
down1Props.id = 0;
MotionEvent.PointerProperties down2Props = new MotionEvent.PointerProperties();
down2Props.id = 1;
MotionEvent.PointerProperties up1Props = new MotionEvent.PointerProperties();
up1Props.id = 0;
MotionEvent.PointerProperties up2Props = new MotionEvent.PointerProperties();
up2Props.id = 1;
List<MotionInputEventParams> allEvents = new ArrayList<>();
allEvents.add(new MotionInputEventParams(0, MotionEvent.ACTION_DOWN, null, 0, down2Props));
allEvents.add(new MotionInputEventParams(0, MotionEvent.ACTION_DOWN, null, 0, down1Props));
allEvents.add(new MotionInputEventParams(0, MotionEvent.ACTION_MOVE, null, 0, null));
allEvents.add(new MotionInputEventParams(0, MotionEvent.ACTION_MOVE, null, 0, null));
allEvents.add(new MotionInputEventParams(0, MotionEvent.ACTION_UP, null, 0, up1Props));
allEvents.add(new MotionInputEventParams(0, MotionEvent.ACTION_UP, null, 0, up2Props));
List<MotionInputEventParams> normalizedEvents = normalizeSequence(allEvents);
assertThat(normalizedEvents.size(), is(equalTo(6)));
assertThat(normalizedEvents.get(0).actionCode, is(equalTo(MotionEvent.ACTION_DOWN)));
assertThat(normalizedEvents.get(0).properties.id, is(equalTo(0)));
assertThat(normalizedEvents.get(1).actionCode, is(equalTo(MotionEvent.ACTION_DOWN)));
assertThat(normalizedEvents.get(1).properties.id, is(equalTo(1)));
assertThat(normalizedEvents.get(2).actionCode, is(equalTo(MotionEvent.ACTION_UP)));
assertThat(normalizedEvents.get(2).properties.id, is(equalTo(1)));
assertThat(normalizedEvents.get(3).actionCode, is(equalTo(MotionEvent.ACTION_UP)));
assertThat(normalizedEvents.get(3).properties.id, is(equalTo(0)));
assertThat(normalizedEvents.get(4).actionCode, is(equalTo(MotionEvent.ACTION_MOVE)));
assertThat(normalizedEvents.get(5).actionCode, is(equalTo(MotionEvent.ACTION_MOVE)));
}
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 MotionEvent.PointerProperties createPointerProps(int id, int toolType) {
MotionEvent.PointerProperties pointerProps = new MotionEvent.PointerProperties();
pointerProps.id = id;
pointerProps.toolType = toolType;
return pointerProps;
}
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);
}
});
}
public static MotionEvent.PointerProperties createProperties(int id) {
MotionEvent.PointerProperties pointerProperties = new MotionEvent.PointerProperties();
pointerProperties.id = id;
pointerProperties.toolType = MotionEvent.TOOL_TYPE_UNKNOWN;
return pointerProperties;
}
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;
}