android 增加SeekBar的拖动区域,更容易拖拉

布局文件参考

    
        
    

代码文件

findViewById(R.id.sb_parent_layout).setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Rect seekRect = new Rect();
            mSeekBar.getHitRect(seekRect);
            if ((event.getY() >= (seekRect.top - 500)) && (event.getY() <= (seekRect.bottom + 500))) {
                float y = seekRect.top + seekRect.height() / 2;
                //seekBar only accept relative x
                float x = event.getX() - seekRect.left;
                if (x < 0) {
                    x = 0;
                } else if (x > seekRect.width()) {
                    x = seekRect.width();
                }
                MotionEvent me = MotionEvent.obtain(event.getDownTime(), event.getEventTime(),
                        event.getAction(), x, y, event.getMetaState());
                return mSeekBar.onTouchEvent(me);
            }
            return false;
        }
    });

你可能感兴趣的:(android 增加SeekBar的拖动区域,更容易拖拉)