如何在 Android 中对旋转图像进行命中测试?

发布于 2024-12-08 03:14:28 字数 505 浏览 0 评论 0原文

我正在尝试对表面视图中在画布上绘制的旋转图像对象进行命中测试。 当图像不旋转时,我们可以直接使用 left、top、width 和 height 作为边界来检查该点是否位于图像矩形内。但是当物体旋转到某个角度时怎么办呢?

我正在使用:canvas.rotate(angle,pivotX,pivotY);来绘制旋转图像。

我无法获得图像对象的旋转左侧和顶部。我尝试拍摄原始图像的左侧和顶部,当我点击屏幕时,我使用以下方法以相同的角度旋转触摸点:

angledTouchX = (float) (eventX * Math.cos(-objectAngle) - eventY * Math.sin(-objectAngle));
angledTouchY  = (float) (eventY * Math.sin(-objectAngle) + eventX * Math.cos(-objectAngle));

它不起作用,因为它旋转点 wrt (0,0),但我希望它 wrt图像对象的中心。

I am trying to do hit test on a rotated image object drawn on the canvas in surface view.
When image is not rotate we can directly use left, top , width and height as boundaries to check whether the point lies within image rectangle. But how to do it when the object is rotated to some angle?

I am using: canvas.rotate(angle, pivotX ,pivotY); to draw the rotated image.

I could not get the rotated left and top of the image object. I tried to take original left and top of the image and when i tap on the screen i rotate the touch point back with same angle using:

angledTouchX = (float) (eventX * Math.cos(-objectAngle) - eventY * Math.sin(-objectAngle));
angledTouchY  = (float) (eventY * Math.sin(-objectAngle) + eventX * Math.cos(-objectAngle));

It does not work because it rotates the point wrt (0,0), but i want it wrt center of the image object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷︶言冷语的世界 2024-12-15 03:14:28

正在研究类似的问题。就我而言,我想在接触点与旋转和缩放视图之间进行最热门的操作。因此,我根据视图的旋转来转换触摸点坐标系,并检查其是否位于非旋转视图边界内部。

步骤

  1. 获取从event.getRawX()event.getRawY()接收到的屏幕坐标系中的触摸点

  2. 接收的屏幕位置中的目标视图位置

     int location[] = new int[2];
     view.getLocationOnScreen(位置);
     int viewX = 位置[0];
     int viewY = 位置[1];
    
  3. 创建向量,就好像视图原点是空间原点

    float[] vec = new float[2];
    vec[0] = event.getRawX() - viewX;
    vec[1] = event.getRawY() - viewY;
    
  4. 旋转向量

    矩阵矩阵 = new Matrix();
    矩阵.setRotate(-1 * view.getRotation());
    矩阵.mapVectors(vec);
    
  5. 将旋转矢量更改为屏幕空间

    vec[0] = vec[0] + viewX;
    vec[1] = vec[1] + viewY;
    
  6. 检查旋转触摸坐标是否在视图未旋转边界内

    if(( vec[0] > viewX 
        && vec[0] < (viewX + (view.getWidth() * view.getScaleX()) )) 
        &&( vec[1] > viewY 
        && vec[1] < (viewY + (view.getHeight() * view.getScaleY()) ))){
            isPointInTextView = true;
    } 别的 {
            isPointInTextView = false;
    }
    

Was working on similar problem. In my case, I want to do a hottest between touchpoint and rotated and scaled view. So I transform touch point co-ordinate system according to view's rotation and check if its inside non-rotated view boundary or not.

Step

  1. Get touch point in screen co-ordinate system which received from event.getRawX() and event.getRawY()

  2. Target view location in screen location which received from

     int location[] = new int[2];
     view.getLocationOnScreen(location);
     int viewX = location[0];
     int viewY = location[1];
    
  3. Create vector as if view origin is the space origin

    float[] vec = new float[2];
    vec[0] = event.getRawX() - viewX;
    vec[1] = event.getRawY() - viewY;
    
  4. Rotate the vector

    Matrix matrix = new Matrix();
    matrix.setRotate(-1 * view.getRotation());
    matrix.mapVectors(vec);
    
  5. Change rotated vector to screen space

    vec[0] = vec[0] + viewX;
    vec[1] = vec[1] + viewY;
    
  6. Check if rotated touch co-ordinate is in view unrotated boundary

    if(( vec[0] > viewX 
        && vec[0] < (viewX + (view.getWidth() * view.getScaleX()) )) 
        &&( vec[1] > viewY 
        && vec[1] < (viewY + (view.getHeight() * view.getScaleY()) ))){
            isPointInTextView = true;
    } else {
            isPointInTextView = false;
    }
    
秋凉 2024-12-15 03:14:28

你需要用数学方法来处理它。最好的方法是在旋转画布之前将位置更改为宽度/2、高度/2。然后应用旋转,最后按宽度/2、高度/2 移回之前的位置。那么你的图片将始终从中心旋转。但旋转后,您的新图像仍将具有新的尺寸,适合与屏幕两侧对齐的矩形。

You need to work with it mathematically. the best way to do is before you rotating the canvas change the position by width/2, height/2. Then apply rotation, finally move back to the previous location by width/2, height/2. Then your picture will be always rotated from the center. But still after rotating your new image will have a new size which fits as a rectagle align to screen sides.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文