【问题标题】:How to know if two images are intersect while one image moving in android?如何知道两个图像是否相交而一个图像在android中移动?
【发布时间】:2013-08-23 08:25:59
【问题描述】:

在我的应用程序中,我使用onTouchListener 在屏幕上移动图像。

我在同一个视图中还有另外两个图像。 我的问题是,当运动图像触及任何其他图像时,我需要执行某个动作 (这意味着如果图像相交,则做一些事情)。

如何做到这一点?请尽快帮助我

提前致谢。

【问题讨论】:

  • 为什么不为每个单独的触摸监听器使用?

标签: android android-imageview android-image ontouchlistener android-gesture


【解决方案1】:

你应该可以使用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
} 

参考是here

【讨论】:

  • @user2681425 :这在我的情况下不起作用。你能用解决方案更新你的问题吗?
【解决方案2】:

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

得到 Rect 的约束:

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

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

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

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

【讨论】:

    【解决方案3】:

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

    a.getLeft() <= b.getRight() &&
    b.getLeft() <= a.getRight() &&
    a.getTop() <= b.getBottom() &&
    b.getTop() <= a.getBottom()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      相关资源
      最近更新 更多