Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将本机UIManager.measureInWindow coords响应到android MotionEvent coords_Javascript_Java_Android_React Native - Fatal编程技术网

Javascript 将本机UIManager.measureInWindow coords响应到android MotionEvent coords

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函数。出

我使用的是一个本机函数,使用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函数。出于某种原因,当我使用“测量视图位置”时,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);
  });