Android expands the range of the click event receiving area

Android expands the range of the click event receiving area

If there are some icons or ImageViews, these Views themselves are very small, so expand the receiving area of ​​these events. Directly pass in the view that needs to expand the event receiving area, or you can customize how much size to expand. The default method is to add 20pix around the original View.

    public static void expandTouchArea(View view) {
        setTouchDelegate(view, 20);
    }

    public static void expandTouchArea(View view, int size) {
        View parentView = (View) view.getParent();
        parentView.post(new Runnable() {
            @Override
            public void run() {
                Rect rect = new Rect();
                view.getHitRect(rect);

                rect.top -= size;
                rect.bottom += size;
                rect.left -= size;
                rect.right += size;

                parentView.setTouchDelegate(new TouchDelegate(rect, view));
            }
        });
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325388352&siteId=291194637