美文网首页
SeekBar可拖动不可点击,滑块禁止点击快进,扩大滑块区域,滑

SeekBar可拖动不可点击,滑块禁止点击快进,扩大滑块区域,滑

作者: 铁肩侠 | 来源:发表于2019-08-02 09:59 被阅读0次

    SeekBar可拖动不可点击,滑块禁止点击快进

    先附上github链接:https://github.com/feiyuu/NoClickSeekBar

    no bb 直接上代码

     @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (!mIsUserSeekable || !isEnabled()) {
                return false;
            }
     
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if (isInScrollingContainer()) {
                        mTouchDownX = event.getX();
                    } else {
                        startDrag(event);
                    }
                    break;
     
                case MotionEvent.ACTION_MOVE:
                    if (mIsDragging) {
                        trackTouchEvent(event);
                    } else {
                        final float x = event.getX();
                        if (Math.abs(x - mTouchDownX) > mScaledTouchSlop) {
                            startDrag(event);
                        }
                    }
                    break;
     
                case MotionEvent.ACTION_UP:
                    if (mIsDragging) {
                        trackTouchEvent(event);
                        onStopTrackingTouch();
                        setPressed(false);
                    } else {
                        // Touch up when we never crossed the touch slop threshold should
                        // be interpreted as a tap-seek to that location.
                        onStartTrackingTouch();
                        trackTouchEvent(event);
                        onStopTrackingTouch();
                    }
                    // ProgressBar doesn't know to repaint the thumb drawable
                    // in its inactive state when the touch stops (because the
                    // value has not apparently changed)
                    invalidate();
                    break;
     
                case MotionEvent.ACTION_CANCEL:
                    if (mIsDragging) {
                        onStopTrackingTouch();
                        setPressed(false);
                    }
                    invalidate(); // see above explanation
                    break;
            }
            return true;
        }
    

    这是seekbar父类里面处理滑动和点击事件的代码。自定义seekbar重写onTouchEvent不调用super就执行不到这里,也就没有拖动和点击事件了。

    判断是DOWN事件是否在thumb滑块上,如果不在就不处理事件,就能实现禁止点击快进进度条了。

    判断是否在thumb上的代码
    (这里需要加上ThumbOffset和padding的距离,高度不用管因为thumb占满高度,设置个背景就能看出来):

        /**
         * 判断MotionEvent事件是否位于thumb上
         *
         * @param event
         * @param thumbBounds
         * @return
         */
        private boolean isTouchInThumb(MotionEvent event, Rect thumbBounds) {
            float x = event.getX();
            float y = event.getY();
            //根据偏移量和左边距确定thumb位置
            int left = thumbBounds.left - getThumbOffset() + getPaddingLeft();
            int right = left + thumbBounds.width();
            if (x >= left && x <= right
                    && y >= thumbBounds.top && y <= thumbBounds.bottom)
                return true;
            return false;
        }
    自定义seekBar的onTouchEvent:
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
     
                    if (!isTouchInThumb(event, getThumb().getBounds())) {
                        return false;
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    break;
                case MotionEvent.ACTION_UP:
                    break;
            }
            return super.onTouchEvent(event);
        }
    

    嫌滑块区域太小,操作拖动不流畅?看如何增大触摸区域吧:

    seekbar用shape图像设置thumb滑块图片,要想扩大点击区域同时不改变滑块大小。如果通过设置maxheight或者padding在一些手机上确实能达到效果,但是在部分手机上会出现意想不到的UI效果(亲测!!!),so最好是\color{red}{通过stroke属性设置透明描边增加点击区域}(亲测兼容,稳定,好使!)。

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="oval">
     
        <solid android:color="@color/colorPrimary"></solid>
     
        <size
            android:width="60dp"
            android:height="60dp"
            ></size>
     
        <stroke android:width="50dp"
            android:color="@android:color/transparent"
            ></stroke>
     
     
    </shape>
    

    给滑块设置等待动画

      //seekbar滑块动画
            mRotateDrawable = (RotateDrawable) getResources().getDrawable(R.drawable.audio_loading_progress);
     
            mValueAnimator = ValueAnimator.ofInt(0, 10000);
            mValueAnimator.setDuration(6000);
            mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    Integer level = (Integer) animation.getAnimatedValue();
                    level = level + 200;
                    mRotateDrawable.setLevel(level);
                }
            });
            mValueAnimator.setRepeatMode(ValueAnimator.RESTART);
            mValueAnimator.setRepeatCount(ValueAnimator.INFINITE);
            mValueAnimator.setInterpolator(new LinearInterpolator());
     
            mSeekBar.setThumb(mRotateDrawable);
            mValueAnimator.start();
    

    drawable:

    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
            android:drawable="@drawable/seekbar_thumb_wait"
            android:fromDegrees="0"
            android:toDegrees="360"
            android:pivotX="50%"
            android:pivotY="50%"
            android:repeatCount="-1">
    </rotate>
    

    github链接:https://github.com/feiyuu/NoClickSeekBar

    相关文章

      网友评论

          本文标题:SeekBar可拖动不可点击,滑块禁止点击快进,扩大滑块区域,滑

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