有没有简单的方法来检查两个ImageView是否发生冲突?

时间:2018-02-12 12:44:20

标签: android collision

我试过了:

   if (pic_runner.getX() + pic_runner.getY() == pic_a.getX() + pic_a.getY()){
                Toast.makeText(getApplicationContext(), "KOLLIDIERT!", Toast.LENGTH_SHORT).show();
            }

但那没用。

我和&&运营商,但后来我得到错误“运营商”&&'不能应用于'float','boolean'“

1 个答案:

答案 0 :(得分:1)

要检查两个ImageView的碰撞,您可以执行以下操作:

{
    ImageView pic1 = (ImageView) findViewById(R.id.pic1);
    ImageView pic2 = (ImageView) findViewById(R.id.pic2);

    Rect pic1Rect = new Rect();
    Rect pic2Rect = new Rect();

    pic1.getDrawingRect(pic1Rect);
    pic2.getDrawingRect(pic2Rect);

    Log.e("TEST", "hasCollision: " + hasCollision(pic1Rect, pic2Rect));
}


public static boolean hasCollision(Rect one, Rect two) {
    return (one.left < two.right &&
            one.right > two.left &&
            one.top < two.bottom &&
            one.bottom > two.top);
}

编辑

public class MainActivity extends AppCompatActivity {

    private ImageView pic1;
    private ImageView pic2;

    private Rect pic1Rect = new Rect();
    private Rect pic2Rect = new Rect();

    private boolean collisionEventHandled = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pic1 = (ImageView) findViewById(R.id.pic1);
        pic2 = (ImageView) findViewById(R.id.pic2);

        RelativeLayout main = (RelativeLayout) findViewById(R.id.main);
        Button btn = (Button) findViewById(R.id.button);

        main.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                pic2.setX(event.getX());
                pic2.setY(event.getY());
                return true;
            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pic1Rect.left = (int) pic1.getX();
                pic1Rect.top = (int) pic1.getY();
                pic1Rect.right = (int) pic1.getX() + pic1.getWidth();
                pic1Rect.bottom = (int) pic1.getY() + pic1.getHeight();

                pic2Rect.left = (int) pic2.getX();
                pic2Rect.top = (int) pic2.getY();
                pic2Rect.right = (int) pic2.getX() + pic2.getWidth();
                pic2Rect.bottom = (int) pic2.getY() + pic2.getHeight();

                Log.e("TEST", "handleCollision: " + handleCollision(pic1Rect, pic2Rect));
            }
        });
    }

    private boolean handleCollision(Rect one, Rect two) {
        boolean hasCollision = hasCollision(one, two);
        if (collisionEventHandled != hasCollision) {
            collisionEventHandled = hasCollision;
            return hasCollision;
        }
        return false;
    }

    private static boolean hasCollision(Rect one, Rect two) {
        return (one.left < two.right &&
                one.right > two.left &&
                one.top < two.bottom &&
                one.bottom > two.top);
    }
}

和xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusable="true"
            android:focusableInTouchMode="true">

    <ImageView
        android:id="@+id/pic1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:background="@android:color/holo_red_dark"/>

    <ImageView
        android:id="@+id/pic2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@android:color/holo_green_light"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>