在Android工作室中碰撞ImageView

时间:2016-05-16 03:50:25

标签: android android-studio android-imageview collision-detection

请帮帮我,在android studio中的两个ImageView之间进行代码冲突。 非常感谢你  但它没有检测到碰撞,它总是注意到Not Intersected,请帮帮我

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_second);                            


            int x = img_animation.getRight() - img_animation.getLeft();
            int y = img_animation.getBottom() - img_animation.getTop();
            final TranslateAnimation translate = new TranslateAnimation(
                    Animation.ABSOLUTE, x, Animation.ABSOLUTE,
                    2000, Animation.ABSOLUTE, 0,
                    Animation.ABSOLUTE, y);
            translate.setDuration(3200);//speed of the animation
            translate.setFillEnabled(true);
            translate.setFillAfter(true);
            img_animation.startAnimation(translate);

    //2nd brick animation

            int x1 = img_animation1.getRight() - img_animation1.getLeft();
            int y1 = img_animation1.getBottom() - img_animation1.getTop();
            final TranslateAnimation secondone = new TranslateAnimation(
                    Animation.ABSOLUTE, x1, Animation.ABSOLUTE,
                    -2000, Animation.ABSOLUTE, 0,
                    Animation.ABSOLUTE, y1);
            secondone.setDuration(5200);//speed of the animation
            secondone.setFillEnabled(true);
            secondone.setFillAfter(true);
            img_animation1.startAnimation(secondone);

                Rect R1=new Rect(img_animation.getLeft(), img_animation.getTop(),img_animation.getRight(),img_animation.getBottom());
                img_animation.getHitRect(R1);
                Rect R2=new Rect(img_animation1.getLeft(),img_animation1.getTop(),img_animation1.getRight(),img_animation1.getBottom());
                img_animation1.getHitRect(R2);

                if (Rect.intersects(R1, R2)) {

                    Toast.makeText(this, "Intersected!", Toast.LENGTH_SHORT).show();
                } else {

                    Toast.makeText(this, "Not Intersected!", Toast.LENGTH_SHORT).show();
                }
        }

1 个答案:

答案 0 :(得分:0)

尝试

int x = img_animation.getX();
int y = img_animation.getY();