Javascript 将本机UIManager.measureInWindow coords响应到android MotionEvent coords
我使用的是一个本机函数,使用MotionEvent提供的坐标可以很好地工作:Javascript 将本机UIManager.measureInWindow coords响应到android MotionEvent coords,javascript,java,android,react-native,Javascript,Java,Android,React Native,我使用的是一个本机函数,使用MotionEvent提供的坐标可以很好地工作: @Override public boolean onTap(MotionEvent e) { int x = Math.round(e.getX()); int y = Math.round(e.getY()); myFunction(x, y); } 我需要做的是在react native中获取视图的坐标,以便能够使用相同的java函数。出
@Override
public boolean onTap(MotionEvent e) {
int x = Math.round(e.getX());
int y = Math.round(e.getY());
myFunction(x, y);
}
我需要做的是在react native中获取视图的坐标,以便能够使用相同的java函数。出于某种原因,当我使用“测量视图位置”时,x和y的比例似乎与MotionEvent e.getX()和e.getY()不同
这是我在React Native组件中使用的:
UIManager.measureInWindow(findNodeHandle(this.view),
(x, y, w, h) => {
this.myNativeComponent.mapDeviceCoordsToPage(x, y);
});
在做了一些研究之后,我发现答案是正确的。UIManager.measureInWindow回调返回的坐标是布局坐标。要获得MotionEvent之类的像素坐标,需要使用react native的PixelRatio.getPixelSizeForLayoutSize函数。因此,我的最终解决方案如下所示:
UIManager.measureInWindow(findNodeHandle(this.view),
(x, y, w, h) => {
const pixelX = PixelRatio.getPixelSizeForLayoutSize(x);
const pixelY = PixelRatio.getPixelSizeForLayoutSize(y);
this.myNativeComponent.mapDeviceCoordsToPage(pixelX, pixelY);
});