这是我在Android中碰撞两个ImageView时必须检测的代码:
Rect rc1 = new Rect();
brickimg.getDrawingRect(rc1);
Rect rc2 = new Rect();
playerimage.getDrawingRect(rc2);
if (Rect.intersects(rc1, rc2)) {
// intersection is detected
// here is your method call
button.setText("Shrekt m9");
}
Rect rc3 = new Rect();
brickimg2.getDrawingRect(rc3);
Rect rc4 = new Rect();
playerimage2.getDrawingRect(rc3);
if (Rect.intersects(rc3, rc4)) {
// intersection is detected
// here is your method call
button.setText("this works");
}
if(Rect.intersects(rc2, rc3)){
button.setText("this works too");
}
当我运行应用程序时,它没有检测到任何内容。难道我做错了什么?
答案 0 :(得分:1)
尝试这样:
将您的Rect
更改为您创建的矩形对象之一。
if (rc1.intersects( rc2)) {
// intersection is detected
// here is your method call
button.setText("Shrekt m9");
}
Rect rc3 = new Rect();
brickimg2.getDrawingRect(rc3);
Rect rc4 = new Rect();
playerimage2.getDrawingRect(rc3);
if (rc3.intersects( rc4)) {
// intersection is detected
// here is your method call
button.setText("this works");
}
if(rc2.intersects( rc3)){
button.setText("this works too");
}