[英]React native UIManager.measureInWindow coords to android MotionEvent coords
我使用的是本機函數,使用從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);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.