5

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

Thanks in Advance.

4

3 回答 3

14

您应该能够使用Rect.intersects(Rect, Rect),例如以下示例:

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
} 

参考在这里

于 2013-08-23T08:51:32.267 回答
1

在带有移动动作的 onTouch 中,您可以获得移动图像和其他图像的矩形边界。通过 intersect 函数检查您的移动矩形是否与另一个相交,例如: Rect movingBound = new Rect(); Rect[] anotherImagesBound = new Rect[...]

得到 Rect 的约束:

Rect movingBound = new Rect();
movingImage.getHitRect(movingBound);

与另一个 imageView 相同。在 anotherImagesBound 中循环并检查:

if (anotherImagesBound[index].intersect(movingBound)){ 
  // do something here
}

注意:您必须在每个触摸动作中更新movingBound,但您应该获得一次另一个ImageView。希望这有帮助

于 2013-08-23T09:00:16.360 回答
0

当您在 onTouch 侦听器中移动图像时,请使用以下命令检查 View a 和 View b 之间的矩形交集:

a.getLeft() <= b.getRight() &&
b.getLeft() <= a.getRight() &&
a.getTop() <= b.getBottom() &&
b.getTop() <= a.getBottom()
于 2013-08-23T08:47:25.183 回答