0

我现在正在做一个项目,我有两个可以在屏幕上拖动的圆圈,当圆圈被拖动到另一个圆圈上时,它们的颜色会改变。

如何检测一个圆圈何时被拖动到另一个圆圈上?提前致谢。

这是我的 MainActivity.java

public class MainActivity extends Activity {
int windowwidth;
int windowheight;

TextView results;

private RelativeLayout.LayoutParams layoutParams;

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

    windowwidth = getWindowManager().getDefaultDisplay().getWidth();
    windowheight = getWindowManager().getDefaultDisplay().getHeight();

    RelativeLayout rel = (RelativeLayout) findViewById(R.id.mainLayout);
    final Balls ball1 = new Balls(this);
    final Balls ball2 = new Balls(this);

    rel.addView(ball1);
    rel.addView(ball2);
    results = (TextView) findViewById(R.id.textView1);

    ball1.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub

            layoutParams = (LayoutParams) ball1.getLayoutParams();
            switch (event.getActionMasked()) {
            case MotionEvent.ACTION_UP:

                break;

            case MotionEvent.ACTION_MOVE:
                int x_cord = (int) event.getRawX();
                int y_cord = (int) event.getRawY();
                if (x_cord > windowwidth) {
                    x_cord = windowwidth;
                }
                if (y_cord > windowheight) {
                    y_cord = windowheight;
                }
                layoutParams.leftMargin = x_cord - 25;
                layoutParams.topMargin = y_cord - 75;
                ball1.setLayoutParams(layoutParams);



                break;

            default:
                break;
            }
            return true;
        }
    });

    ball2.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            layoutParams = (LayoutParams) ball2.getLayoutParams();
            switch (event.getActionMasked()) {
            case MotionEvent.ACTION_UP:

                break;

            case MotionEvent.ACTION_MOVE:
                int x_cord = (int) event.getRawX();
                int y_cord = (int) event.getRawY();

                if (x_cord > windowwidth) {
                    x_cord = windowwidth;
                }
                if (y_cord > windowheight) {
                    y_cord = windowheight;
                }
                layoutParams.leftMargin = x_cord - 25;
                layoutParams.topMargin = y_cord - 75;
                ball2.setLayoutParams(layoutParams);
                ;
                break;

            default:
                break;
            }
            // TODO Auto-generated method stub
            return true;
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}}

我的球.java

public class Balls extends TextView{
public Balls(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    this.setBackgroundResource(R.drawable.bgshape);
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:id="@+id/mainLayout"></RelativeLayout>

bgshape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<padding
    android:bottom="20dp"
    android:left="25dp"
    android:right="25dp"
    android:top="20dp" />
<stroke
    android:width="2dp"
    android:color="#000000" />
<solid android:color="#ffffff" />
<corners
    android:bottomLeftRadius="30dp"
    android:bottomRightRadius="30dp"
    android:topLeftRadius="30dp"
    android:topRightRadius="30dp" /></shape>
4

1 回答 1

0

假设你有两个圆圈,A 和 B。

你触摸并拖动A,你会得到A和B的坐标,对吧?

由于你有B的坐标,你可以Region.contains(int x, int y)用来判断A和B是否重叠。

参考

于 2012-10-12T08:08:22.987 回答