如何测试Android中的对象是否重叠

时间:2014-03-02 17:46:29

标签: android object overlapping

我试图制作一个游戏,屏幕上的方块用按钮向上,向下,向左或向右移动,当广场遇到另一个随机放置的方格时,方块会移动。但是,我不知道如何测试两个ImageView是否重叠。请保持答案简单易懂。我是android的新手。如果您需要,我的代码如下:

package com.example.runaway;
import android.os.Bundle;
import android.app.Activity;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

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

    final ImageView box = (ImageView) findViewById(R.id.player);
    Button pushMedown = (Button) findViewById(R.id.down);



        pushMedown.setOnClickListener(new View.OnClickListener() { 

            int location = (int) box.getY();
            int math = 55;
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            location = (int) (math + box.getY()); 
            box.setY(location);
            if (location > 400) {location = location - math;}
            box.setY(location);


        }
    });
                                // up
        final ImageView box1 = box;
        Button pushMeup = (Button) findViewById(R.id.up);



        pushMeup.setOnClickListener(new View.OnClickListener() { 


            int location = (int) box.getY();
            int math = 55;
    @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        location = (int) (box.getY() - math); 
        box.setY(location);
        if (location < 10) {location = location + math;}
        box.setY(location);

    }
        });

                            // right
            final ImageView box2 = box;
            Button pushMeleft = (Button) findViewById(R.id.right);



            pushMeleft.setOnClickListener(new View.OnClickListener() { 


                int location1 = (int) box.getX();
                int math = 55;
            @Override
                public void onClick(View v) {
                // TODO Auto-generated method stub

                location1 = (int) (math + box.getX()); 
                box.setX(location1);
                if (location1 > 400) {location1 = location1 - math;}
                box.setX(location1);

                }
            });
                        // left
            final ImageView box3 = box;
            Button pushMeright = (Button) findViewById(R.id.left);



            pushMeright.setOnClickListener(new View.OnClickListener() { 


                    int location1 = (int) box.getX();
                    int math = 55;
                @Override
                    public void onClick(View v) {
                    // TODO Auto-generated method stub

                    location1 = (int) (box.getX() - math); 
                    box.setX(location1);
                    if (location1 < 0) {location1 = location1 + math;}
                    box.setX(location1);

                }});                        

        }

private ImageView findByViewId(int imageview1) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

1 个答案:

答案 0 :(得分:0)

假设您的ImageViews被称为第一和第二。

if((first.getRight()>second.getLeft()&&first.getLeft()<second.getRight())&&first.getTop()>second.getBottom()&&first.getBottom()<second.getTop())
{
    //the imageViews are overlapping)
}