我想让SeekBar的移动更加平滑,而不是直接跳到位置。我正在这样做,因为我有一个具有3个选项的SeekBar,用于实现滑块类型的实现。
我希望创建一个漂亮流畅的滑块,其具有3个选项“是”,“无选项”,“否”,这是我的应用程序中的一个功能,并且SeekBar看起来是最好和最容易的方法。
我尝试查看Android Animation,但似乎有点复杂,所以如果有人能提供任何帮助,那将是很好的:)
我发现了以下解决方案,可以使SeekBar平滑移动,但仍然将其捕捉在有限的值范围内。假设您的布局中具有以下视图:
<SeekBar
android:id="@+id/sldProgress"
style="@style/MySliderStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50" />
<TextView
android:id="@+id/lblProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />
您可以在活动中使用以下代码(根据需要更改上面xml中的android:max值和下面代码中的平滑系数 - 较高的值=更平滑):
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sliderListener sldListener = new sliderListener();
SeekBar sldProgress = (SeekBar) findViewById(R.id.sldProgress);
sldProgress.setOnSeekBarChangeListener(sldListener);
}
private class sliderListener implements OnSeekBarChangeListener {
private int smoothnessFactor = 10;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = Math.round(progress / smoothnessFactor);
TextView lblProgress = (TextView) findViewById(R.id.lblProgress);
lblProgress.setText(String.valueOf(progress));
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
seekBar.setProgress(Math.round((seekBar.getProgress() + (smoothnessFactor / 2)) / smoothnessFactor) * smoothnessFactor);
}
}
}
您不需要自己构建任何东西,只需使用Android动画即可为您完成
代码用Kotlin编写
mySeekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
//Todo: code
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
if (seekBar.progress > 70) {
ObjectAnimator.ofInt(seekBar, "progress", 100).setDuration(100).start()
} else if (seekBar.progress < 30) {
ObjectAnimator.ofInt(seekBar, "progress", 0).setDuration(100).start()
} else {
ObjectAnimator.ofInt(seekBar, "progress", 50).setDuration(100).start()
}
}
})
我自己构建了控件。工作完成 :) 代码在这里
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);
}
}
}
<com.yourPackage.SmoothSeekBar
android:id="@+id/seekBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
if (currentPosition > 0) {
ObjectAnimator animation = ObjectAnimator.ofInt(seekBar, "progress", currentPosition);
animation.setDuration(400);
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
}
https://github.com/WoochanLee/SmoothSeekBar
dependencyResolutionManagement {
...
repositories {
...
maven { url "https://jitpack.io" }
}
}
dependencies {
...
implementation 'com.github.WoochanLee:SmoothSeekBar:1.0.0'
}
<com.woody.lee.library.smoothseekbar.SmoothSeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content" />