Android get bounding rectangle of a View

后端 未结 5 608
渐次进展
渐次进展 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;
    }
    

提交回复
热议问题