-2
我嘗試用其他元素推送一個元素,但沒有出現。 此外,我想動畫,將彼此相交 當我移動圖像中的一個或兩個沒有任何反應在android中移動一個圖像時相交其他視圖元素?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
box.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
dX = v.getX() - event.getRawX();
dY = v.getY() - event.getRawY();
return true;
case MotionEvent.ACTION_MOVE:
box.animate()
.x(event.getRawX() + dX)
.y(event.getRawY() + dY)
.setDuration(0)
.start();
if (viewsOverlap(box, box2)) {
Log.d("TAG", "getX = " + box.getX() + "getY = " + box.getY());
}
velocityTracker.addMovement(event);
return true;
}
return false;
}
});
box2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
box2.getRight(), box2.getBottom());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
dX2 = v.getX() - event.getRawX();
dY2 = v.getY() - event.getRawY();
return true;
case MotionEvent.ACTION_MOVE:
box2.animate()
.x(event.getRawX() + dX2)
.y(event.getRawY() + dY2)
.setDuration(0)
.start();
if (viewsOverlap(box, box2)) {
Log.d("TAG", "getX2 = " + box2.getX() + "getY2 = " + box2.getY());
}
velocityTracker.addMovement(event);
return true;
}
return false;
}
});
}
private boolean viewsOverlap(View v1, View v2) {
int[] v1_coords = new int[2];
v1.getLocationOnScreen(v1_coords);
int v1_w = v1.getWidth();
int v1_h = v1.getHeight();
Rect v1_rect = new Rect(v1_coords[0], v1_coords[1], v1_coords[0] + v1_w, v1_coords[1] + v1_h);
//textView.setText("v1[0]= " + v1_coords[0] +" v1[1]= " + v1_coords[1] + "v1[0] + w= " + (v1_coords[0] + v1_w)+ "v1[1]+h= " + (v1_coords[1] + v1_h));
int[] v2_coords = new int[2];
v2.getLocationOnScreen(v1_coords);
int v2_w = v2.getWidth();
int v2_h = v2.getHeight();
Rect v2_rect = new Rect(v2_coords[0], v2_coords[1], v2_coords[0] + v2_w, v2_coords[1] + v2_h);
//textView2.setText("v1[0]= " + v1_coords[0] +" v1[1]= " + v1_coords[1] + "v1[0] + w= " + (v1_coords[0] + v1_w)+ "v1[1]+h= " + (v1_coords[1] + v1_h));
return v1_rect.intersect(v2_rect) || v1_rect.contains(v2_rect) || v2_rect.contains(v1_rect);
}
但吐司從來沒有表現出的元素。 有什麼問題?
你有沒有登錄'boxRect'和'box2Rect'的價值? – azizbekian
現在看起來像 –
@azizbekian是的..我更新我的代碼..我不明白 –