Android get bounding rectangle of a View

后端 未结 5 624
渐次进展
渐次进展 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: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.

提交回复
热议问题