简体   繁体   English

将本机UIManager.measureInWindow坐标反应为android MotionEvent坐标

[英]React native UIManager.measureInWindow coords to android MotionEvent coords

I am using a native function that works fine using the coords i get from MotionEvent: 我使用的是本机函数,使用从MotionEvent获得的坐标可以正常工作:

    @Override
    public boolean onTap(MotionEvent e) {

        int x = Math.round(e.getX());
        int y = Math.round(e.getY());
        myFunction(x, y);
    }

What i need to do, is to get the coordinates of a view in react native to be able to use the same java function. 我需要做的是在react native中获取视图的坐标,以便能够使用相同的Java函数。 For some reason when i use measure view position it seems that x and y are in a different scale than MotionEvent e.getX() and e.getY(). 出于某种原因,当我使用量度视图位置时,x和y似乎与MotionEvent e.getX()和e.getY()的比例不同。

This is the i am using in my React Native component: 这是我在我的React Native组件中使用的:

UIManager.measureInWindow(findNodeHandle(this.view),
      (x, y, w, h) => {
         this.myNativeComponent.mapDeviceCoordsToPage(x, y);
      });

After doing some reasearch i found the answer in this article . 经过一些研究,我在本文中找到了答案。 The coordinates returned by UIManager.measureInWindow's callback are layout coordinates. UIManager.measureInWindow的回调返回的坐标是布局坐标。 To get the pixel coordinates like MotionEvent you need to use react native's PixelRatio.getPixelSizeForLayoutSize function. 为了获得像MotionEvent这样的像素坐标,您需要使用react native的PixelRatio.getPixelSizeForLayoutSize函数。 So my final solution looks like this: 所以我的最终解决方案如下所示:

UIManager.measureInWindow(findNodeHandle(this.view),
  (x, y, w, h) => {
     const pixelX = PixelRatio.getPixelSizeForLayoutSize(x);
     const pixelY = PixelRatio.getPixelSizeForLayoutSize(y);
     this.myNativeComponent.mapDeviceCoordsToPage(pixelX, pixelY);
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM