【问题标题】:Android ImageView Collision Detection?Android ImageView 碰撞检测?
【发布时间】:2014-09-15 12:50:06
【问题描述】:

当两个 ImageViews 相互交叉时,我试图让某些事情发生。这是我使用的代码:

Rect rect1 = new Rect();
imageView.getHitRect(rect1);
Rect rect4 = new Rect();
imageView2.getHitRect(rect4);

boolean collision = false; 
collision = rect1.intersect(rect4);
if(collision = true){
    button.setText("collided"); 
}else
    button.setText("not collided"); 

但是,当应用启动时,布尔值刚刚更改为 true。第一个 ImageView 保持静止,而另一个向第一个移动(它不是精灵,但它向第一个 ImageView 的方向移动并经过它)。我希望在两个 ImageView 相交时更改布尔值。有什么我想念的吗?

【问题讨论】:

    标签: android imageview collision


    【解决方案1】:

    试试这个:

     Rect rc1 = new Rect();
     imageView1.getDrawingRect(rc1);
     Rect rc2 = new Rect();
     imageView2.getDrawingRect(rc2);
     if (Rect.intersects(rc1, rc2) {
       // intersection is detected
       // here is your method call
     }
    

    【讨论】:

    • 太糟糕了,您的 awnser 没有作为正确的提交。今天对我有帮助。非常感谢。我必须将此代码放在我的计时器滴答事件之外才能让它工作。不知道为什么。对此有什么想法吗?
    【解决方案2】:

    我在互联网上搜索了几天,这对我有用。惊人的。

    记得每秒检查一下是否有碰撞

    How to know if two images are intersect while one image moving in android?

    【讨论】:

      猜你喜欢
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 2019-04-30
      相关资源
      最近更新 更多