我现在正在开展一个项目,我可以在屏幕上拖动两个圆圈,当圆圈拖过另一个圆圈时,它们的颜色会发生变化。
如何检测圆圈在另一个圆圈上拖动的时间?提前谢谢。
这是我的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;
}}
我的Balls.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>
答案 0 :(得分:0)
假设您有两个圈子,A和B.
你触摸并拖动A,你会得到A和B的坐标,对吗?
由于你有B的坐标,你可以使用Region.contains(int x, int y)
来判断A和B是否重叠。