提交 21690865 编写于 作者: A Amir Hardon 提交者: amirh

Synthesize Android MotionEvents with the long form obtain method.

The simple form MotionEvent.obtain method I used before does not allow
to synthesize multi pointer events.
上级 0b443276
......@@ -15,10 +15,14 @@ import io.flutter.plugin.common.StandardMethodCodec;
import io.flutter.view.FlutterView;
import io.flutter.view.TextureRegistry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static android.view.MotionEvent.PointerCoords;
import static android.view.MotionEvent.PointerProperties;
/**
* Manages platform views.
* <p>
......@@ -191,19 +195,26 @@ public class PlatformViewsController implements MethodChannel.MethodCallHandler
private void onTouch(MethodCall call, MethodChannel.Result result) {
List<Object> args = call.arguments();
float density = mFlutterView.getContext().getResources().getDisplayMetrics().density;
int id = (int) args.get(0);
int downTime = (int) args.get(1);
int eventTime = (int) args.get(2);
int action = (int) args.get(3);
double x = (double) args.get(4);
double y = (double) args.get(5);
double pressure = (double) args.get(6);
double size = (double) args.get(7);
int metaState = (int) args.get(8);
double xPrecision = (double) args.get(9);
double yPrecision = (double) args.get(10);
int pointerCount = (int) args.get(4);
PointerProperties[] pointerProperties =
parsePointerPropertiesList(args.get(5)).toArray(new PointerProperties[pointerCount]);
PointerCoords[] pointerCoords =
parsePointerCoordsList(args.get(6), density).toArray(new PointerCoords[pointerCount]);
int metaState = (int) args.get(7);
int buttonState = (int) args.get(8);
float xPrecision = (float) (double) args.get(9);
float yPrecision = (float) (double) args.get(10);
int deviceId = (int) args.get(11);
int edgeFlags = (int) args.get(12);
int source = (int) args.get(13);
int flags = (int) args.get(14);
View view = vdControllers.get(id).getView();
if (view == null) {
......@@ -215,27 +226,72 @@ public class PlatformViewsController implements MethodChannel.MethodCallHandler
return;
}
float density = mFlutterView.getContext().getResources().getDisplayMetrics().density;
MotionEvent event = MotionEvent.obtain(
downTime,
eventTime,
action,
(float) x * density,
(float) y * density,
(float) pressure,
(float) size,
pointerCount,
pointerProperties,
pointerCoords,
metaState,
(float) xPrecision,
(float) yPrecision,
buttonState,
xPrecision,
yPrecision,
deviceId,
edgeFlags
edgeFlags,
source,
flags
);
view.onTouchEvent(event);
result.success(null);
}
@SuppressWarnings("unchecked")
private static List<PointerProperties> parsePointerPropertiesList(Object rawPropertiesList) {
List<Object> rawProperties = (List<Object>) rawPropertiesList;
List<PointerProperties> pointerProperties = new ArrayList<>();
for (Object o : rawProperties) {
pointerProperties.add(parsePointerProperties(o));
}
return pointerProperties;
}
@SuppressWarnings("unchecked")
private static PointerProperties parsePointerProperties(Object rawProperties) {
List<Object> propertiesList = (List<Object>) rawProperties;
PointerProperties properties = new MotionEvent.PointerProperties();
properties.id = (int) propertiesList.get(0);
properties.toolType = (int) propertiesList.get(1);
return properties;
}
@SuppressWarnings("unchecked")
private static List<PointerCoords> parsePointerCoordsList(Object rawCoordsList, float density) {
List<Object> rawCoords = (List<Object>) rawCoordsList;
List<PointerCoords> pointerCoords = new ArrayList<>();
for (Object o : rawCoords) {
pointerCoords.add(parsePointerCoords(o, density));
}
return pointerCoords;
}
@SuppressWarnings("unchecked")
private static PointerCoords parsePointerCoords(Object rawCoords, float density) {
List<Object> coordsList = (List<Object>) rawCoords;
PointerCoords coords = new MotionEvent.PointerCoords();
coords.orientation = (float) (double) coordsList.get(0);
coords.pressure = (float) (double) coordsList.get(1);
coords.size = (float) (double) coordsList.get(2);
coords.toolMajor = (float) (double) coordsList.get(3) * density;
coords.toolMinor = (float) (double) coordsList.get(4) * density;
coords.touchMajor = (float) (double) coordsList.get(5) * density;
coords.touchMinor = (float) (double) coordsList.get(6) * density;
coords.x = (float) (double) coordsList.get(7) * density;
coords.y = (float) (double) coordsList.get(8) * density;
return coords;
}
private int toPhysicalPixels(double logicalPixels) {
float density = mFlutterView.getContext().getResources().getDisplayMetrics().density;
return (int) Math.round(logicalPixels * density);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册