如何知道在Android中移动一个图像时两个图像是否相交? [英] How to know if two images are intersect while one image moving in android?
本文介绍了如何知道在Android中移动一个图像时两个图像是否相交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,我正在使用onTouchListener
在屏幕上移动图像.
In my application, i am moving image around the screen by using onTouchListener
.
我在同一视图中还有另外两张图像. 我的问题是,当运动图像触摸其他任何图像时,我需要执行某些操作 (这意味着如果图像相交,则可以执行某些操作).
I have another two images in the same view. My problem is that, when the moving image, touches any of the other images, i need to perform a certain action (that means if images are intersected, then do something).
如何实现?请尽快帮助我
How can this be achieved?.Please help me asap
预先感谢.
推荐答案
您应该能够使用Rect.intersects(Rect, Rect)
,例如以下示例:
You should be able to use Rect.intersects(Rect, Rect)
, like this example:
Rect myViewRect = new Rect();
myView.getHitRect(myViewRect);
Rect otherViewRect1 = new Rect();
otherView1.getHitRect(otherViewRect1);
Rect otherViewRect2 = new Rect();
otherView2.getHitRect(otherViewRect2);
if (Rect.intersects(myViewRect, otherViewRect1)) {
// Intersects otherView1
}
if (Rect.intersects(myViewRect, otherViewRect2)) {
// Intersects otherView2
}
引用为 查看全文