Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 implementation.

I'm looking to create a nice smooth slider with 3 options "Yes", "No option", "No" for a feature in my app and a SeekBar looks the best and easiest way.

I've tried looking at Android Animation and it seems a tad bit complex, so if anyone could be of any help that would be nice :)

like image 764
Joe Simpson Avatar asked Mar 13 '12 20:03

Joe Simpson


People also ask

How to use Seek bar in android?

Android SeekBar is a type of ProgressBar. On touching the thumb on seekbar and dragging it to the right or left, the current value of the progress changes. SeekBar is used for forwarding or backwarding the songs, Video etc. In the setOnSeekBarChangeListener interface is used which provides three methods.

How to get SeekBar progress in android?

In Android, SeekBar is an extension of ProgressBar that adds a draggable thumb, a user can touch the thumb and drag left or right to set the value for current progress. SeekBar is one of the very useful user interface element in Android that allows the selection of integer values using a natural user interface.

How to get value from SeekBar android?

The SeekBar class is a subclass of ProgressBar , which contains a getProgress() method. Calling PRICEbar. getProgress() will return the value you are looking for.

What is Seek bar?

Android SeekBar is a kind of ProgressBar with draggable thumb. The end user can drag the thum left and right to move the progress of song, file download etc. The SeekBar. OnSeekBarChangeListener interface provides methods to perform even handling for seek bar.


1 Answers

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

 <com.yourPackage.SmoothSeekBar
    android:id="@+id/seekBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
like image 55
Ness Tyagi Avatar answered Nov 03 '22 16:11

Ness Tyagi