TouchDelegate扩大View的触摸和点击响应范围,最大不超过其父View范围

 public static void expandViewTouchDelegate(final View view, final int top,
                                               final int bottom, final int left, final int right) {

        if(view.getParent() != null){
            ((View) view.getParent()).post(new Runnable() {
                @Override
                public void run() {
                    Rect bounds = new Rect();
                    view.setEnabled(true);
                    view.getHitRect(bounds);

                    bounds.top -= top;
                    bounds.bottom += bottom;
                    bounds.left -= left;
                    bounds.right += right;

                    TouchDelegate touchDelegate = new TouchDelegate(bounds, view);

                    if (View.class.isInstance(view.getParent())) {
                        ((View) view.getParent()).setTouchDelegate(touchDelegate);
                    }
                }
            });
        }
    }

  

posted @ 2017-07-05 10:28  lichl  阅读(96)  评论(0)    收藏  举报