How can I make an Android seek-bar move smoothly?

前端 未结 5 1364
故里飘歌
故里飘歌 2021-02-04 11:05

I\'m trying to make a SeekBar move more smoothly than just jumping straight to the position. I\'m doing this as I\'ve got a SeekBar with 3 options for a slider-type implementati

5条回答
  •  一个人的身影
    2021-02-04 11:41

    Create a custom SeekBar

    public class SmoothSeekBar extends AppCompatSeekBar implements SeekBar.OnSeekBarChangeListener {
    
    private SeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener;
    
    private boolean mNeedCallListener = true;
    
    private ValueAnimator mAnimator;
    
    public SmoothSeekBar(Context context) {
        super(context);
        init(context);
    }
    
    public SmoothSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
    
    public SmoothSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    
    
    public void init(Context context) {
        Context mContext = context;
    }
    
    @Override
    public void setOnSeekBarChangeListener(
            SeekBar.OnSeekBarChangeListener onSeekBarChangeListener) {
        mOnSeekBarChangeListener = onSeekBarChangeListener;
        super.setOnSeekBarChangeListener(this);
    }
    
    @Override
    public void setProgress(final int progress) {
        final int currentProgress = getProgress();
        if (mAnimator != null) {
            mAnimator.cancel();
            mAnimator.removeAllUpdateListeners();
            mAnimator.removeAllListeners();
            mAnimator = null;
            mNeedCallListener = true;
        }
        mAnimator = ValueAnimator.ofInt(currentProgress, progress);
        mAnimator.setDuration(getResources().getInteger(android.R.integer.config_longAnimTime));
        mAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                if (value == progress) {
                    mNeedCallListener = true;
                } else {
                    mNeedCallListener = false;
                }
                Logger.e("ProgressBar value ", value + "");
                SmoothSeekBar.super.setProgress(value);
            }
        });
        mAnimator.start();
    
    }
    
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (fromUser || mNeedCallListener) {
            if (mOnSeekBarChangeListener != null) {
                mOnSeekBarChangeListener.onProgressChanged(seekBar, progress, fromUser);
            }
        }
    }
    
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        if (mOnSeekBarChangeListener != null) {
            mOnSeekBarChangeListener.onStartTrackingTouch(seekBar);
        }
    }
    
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if (mOnSeekBarChangeListener != null) {
            mOnSeekBarChangeListener.onStopTrackingTouch(seekBar);
        }
    }
    }
    

    Now add SeekBar in xml and enjoy

     
    

提交回复
热议问题