Android get bounding rectangle of a View

后端 未结 5 604
渐次进展
渐次进展 2020-12-08 02:17

I\'m implementing a drag and drop for an Android application. In order to know if the drop happens inside the drop target, I need to know the bounding rectangle of the drop

相关标签:
5条回答
  • 2020-12-08 02:31

    Answering my own question ... 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;
    }
    
    0 讨论(0)
  • 2020-12-08 02:37

    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.

    0 讨论(0)
  • 2020-12-08 02:37

    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);
    }
    
    0 讨论(0)
  • 2020-12-08 02:39

    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)
        }
    
    0 讨论(0)
  • 2020-12-08 02:43

    Using getGlobalVisibleRect:

    val rect = Rect()
    view.getGlobalVisibleRect(rect)
    val isContainedInView = rect.contains(x, y)
    
    0 讨论(0)
提交回复
热议问题