如何在 Android 中对旋转图像进行命中测试?
我正在尝试对表面视图中在画布上绘制的旋转图像对象进行命中测试。 当图像不旋转时,我们可以直接使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正在研究类似的问题。就我而言,我想在接触点与旋转和缩放视图之间进行最热门的操作。因此,我根据视图的旋转来转换触摸点坐标系,并检查其是否位于非旋转视图边界内部。
步骤
获取从
event.getRawX()
和event.getRawY()
接收到的屏幕坐标系中的触摸点从
接收的屏幕位置中的目标视图位置
创建向量,就好像视图原点是空间原点
旋转向量
将旋转矢量更改为屏幕空间
检查旋转触摸坐标是否在视图未旋转边界内
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
Get touch point in screen co-ordinate system which received from
event.getRawX()
andevent.getRawY()
Target view location in screen location which received from
Create vector as if view origin is the space origin
Rotate the vector
Change rotated vector to screen space
Check if rotated touch co-ordinate is in view unrotated boundary
你需要用数学方法来处理它。最好的方法是在旋转画布之前将位置更改为宽度/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.