Android get bounding rectangle of a View
You can also use a Rect here:
private boolean isViewContains(...) {
int[] l = new int[2];
imageView.getLocationOnScreen(l);
Rect rect = new Rect(l[0], l[1], l[0] + imageView.getWidth(), l[1] + imageView.getHeight());
return rect.contains(rx, ry);
}
Less wordy, and possibly faster, but certainly (IMO) more readable.
This code takes into consideration, the perimeter of the Views
involved and only returns true
when the dragged View
is fully inside the drop zone.
public boolean containsView(View dropZone, View draggedView){
// Create the Rect for the view where items will be dropped
int[] pointA = new int[2];
dropZone.getLocationOnScreen(pointA);
Rect rectA = new Rect(pointA[0], pointA[1], pointA[0] + dropZone.getWidth(), pointA[1] + dropZone.getHeight());
// Create the Rect for the view been dragged
int[] pointB = new int[2];
draggedView.getLocationOnScreen(pointB);
Rect rectB = new Rect(pointB[0], pointB[1], pointB[0] + draggedView.getWidth(), pointB[1] + draggedView.getHeight());
// Check if the dropzone currently contains the dragged view
return rectA.contains(rectB);
}
Kotlin Extension solution
Here is a combination of extension getters you can add to quickly get the x/y coordinated or the bounding box of any view
val View.screenLocation
get(): IntArray {
val point = IntArray(2)
getLocationOnScreen(point)
return point
}
val View.boundingBox
get(): Rect {
val (x, y) = screenLocation
return Rect(x, y, x + width, y + height)
}
Yes, View.getLocationOnScreen()
did the trick. For example,
private boolean isViewContains(View view, int rx, int ry) {
int[] l = new int[2];
view.getLocationOnScreen(l);
int x = l[0];
int y = l[1];
int w = view.getWidth();
int h = view.getHeight();
if (rx < x || rx > x + w || ry < y || ry > y + h) {
return false;
}
return true;
}