扩大控件点击范围

在实际开发中,会遇到有些UI比较小,控件占用 的控件小,用户点击不到,那么需要手动扩大控件的点击范围,使其热区扩大

public static void expandViewTouchSize(final View view,final float leftRight){
        if (View.class.isInstance(view.getParent())) {
            ((View) view.getParent()).post(new Runnable() {
                @Override
                public void run() {
                    Rect parentBounds = new Rect();
                    ((View) view.getParent()).setEnabled(true);
                    ((View) view.getParent()).getHitRect(parentBounds);
                    Rect childBounds = new Rect();
                    view.setEnabled(true);
                    view.getHitRect(childBounds);
                    Rect newBounds;
                    int tmpSize = BitmapHelper.dip2px(leftRight);
                    if(childBounds.left - tmpSize >= 0
                            && childBounds.right + tmpSize <= parentBounds.right){
                        newBounds = new Rect(childBounds.left - tmpSize,parentBounds.top,childBounds.right + tmpSize,parentBounds.bottom);
                    }else {
                        newBounds = new Rect(childBounds.left, parentBounds.top, childBounds.right, parentBounds.bottom);
                    }
                    TouchDelegate touchDelegate = new TouchDelegate(newBounds, view);
                    if (View.class.isInstance(view.getParent())) {
                        ((View) view.getParent()).setTouchDelegate(touchDelegate);
                    }
                }
            });
        }
    }

你可能感兴趣的:(扩大控件点击范围)