美文网首页
扩大控件点击范围

扩大控件点击范围

作者: 73a8b2d73e7d | 来源:发表于2016-03-08 18:26 被阅读102次

    在实际开发中,会遇到有些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);
                        }
                    }
                });
            }
        }
    

    相关文章

      网友评论

          本文标题:扩大控件点击范围

          本文链接:https://www.haomeiwen.com/subject/qhuzkttx.html