问题
I have two ImageView
- One is fixed at a position
- On Second I need to apply drag an drop functionality.
What I need:
Drag second ImageView to the another ImageView and when they intersect than I need to call a method.
EDIT 1:
Intersect b/w two imageView is DONE but on the drag n drop it's not happening.
Edit 2 :
How can I achieve this thing on Drag and drop?
回答1:
I assume that you need this functionality for Android. The easiest way is to use intersects method found in Rect class. Link to documentation.
import android.graphics.Rect;
...
Rect rc1 = new Rect();
imageView1.getDrawingRect(rc1);
Rect rc2 = new Rect();
imageView2.getDrawingRect(rc2);
if (Rect.intersects(rc1, rc2)) {
// intersection is detected
// here is your method call
}
*EDIT added missing right bracket
回答2:
This is my Solution that works
private boolean isViewOverlapping(View firstView, View secondView) {
final int[] location = new int[2];
firstView.getLocationInWindow(location);
Rect rect1 = new Rect(location[0], location[1],location[0] + firstView.getWidth(), location[1] + firstView.getHeight());
secondView.getLocationInWindow(location);
Rect rect2 = new Rect(location[0], location[1],location[0] + secondView.getWidth(), location[1] + secondView.getHeight());
return rect1.intersect(rect2);
// return (rect1.contains(rect2)) || (rect2.contains(rect1));
}
call the above function in
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DROP:
if(isViewOverlapping(view,child))
{Toast.makeText(this,"Overlapping with "+child.getTag() or text ,Toast.LENGTH_SHORT).show();
}
Hope this helps
回答3:
@override
onWindowFocusChanged(boolean focus)
and put your code in there cz onCreate()/onStart() - this will not work
回答4:
Do you already have the drag and drop functionality created for the second ImageView?
If so, all you need to do is call getLeft(), getRight(), so on to find the position of your second view after the drag (or on a Handler every few increments while it's being dragged), then this becomes a simple rectangle intersection problem, which already has an answer here.
回答5:
Late reply, but I also had this problem and fixed it with the following code. The solution for Action drop is answered here, so I don´t post it again. But if You want to detect it WHILE DRAGGING , you have to do that stuff inside DragEvent.ACTION_DRAG_LOCATION.
get x y of current drag event:
int x_cord = (int) event.getX(); int y_cord = (int) event.getY();now calculate the size of the rect. For that (in my case) if have to divide the size of the view because the drag position was in the center of it and then calculate the top/left/right/bottom:
int left = (int) (x_cord - halfViewSize); int top = (int) (y_cord - halfViewSize); int right = (int) (x_cord + halfVaieSize); int bottom = (int) (y_cord + halfViewSize);
I casted the value because in my case halfViewSize was a float. If you have an integer, you don´t need to cast.
Now you can build a rect with these values:
Rect rect = new Rect(left, top, right, bottom);create a method which checks if the rect intersects with a rect of your childViews:
private boolean collideWithOthers(Rect rect) { int count = yourParentLayout.getChildCount(); boolean intersects = false; for (int i = 0; i < count; i++) { ImageView squareInside = (ImageView) yourParentLayout.getChildAt(i); Rect rectInside = squareInside.getCollisionRect(); if (rectInside.intersect(rect)) { Log.d("TAG", "ATTENTION INTERSECT!!!"); intersects = true; break;//stop the loop because an intersection is detected } } return intersects; }
Now you can do this stuff inside ACTION_DRAG_LOCATION:
int x_cord = (int) event.getX();
int y_cord = (int) event.getY();
int left = (int) (x_cord - halfViewSize);
int top = (int) (y_cord - halfViewSize);
int right = (int) (x_cord + halfVaieSize);
int bottom = (int) (y_cord + halfViewSize);
Rect rect = new Rect(left, top, right, bottom);
boolean collide = collideWithOthers(rect);
I hope this will fit to your needs, if not, maybe it´s helpful for others.
来源:https://stackoverflow.com/questions/19086776/how-to-find-intersect-between-two-imageview-when-drag-and-drop