使seekBar可滑动不可点击

1,889 阅读1分钟
  1. 禁止SeekBar拖动和点击
@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;
    }
  1. 可拖动不可点击

    我们只需要判断MotionEvent事件是否位于thumb上,在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);
    }
  1. 增大滑块触摸区域
<?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>

  1. 设置滑块的等待动画
//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();

动画

<?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地址 原文链接