我试图使SeekBar的移动更顺畅,而不仅仅是直接跳到这个位置。我正在这样做,因为我有一个SeekBar,有3个滑块类型实现选项。
我正在寻找创建一个漂亮的平滑滑块,其中3个选项为“是”,“无选项”,“否”为我的应用中的功能,而SeekBar看起来是最好最简单的方式。
我已经尝试过看Android动画了,看起来有点复杂,所以如果有人能提供任何帮助那就太好了:)
答案 0 :(得分:12)
我找到了以下解决方案,使搜索栏平滑移动,但仍然可以捕捉到有限范围的值。假设您的布局中包含以下视图:
<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);
}
}
}
答案 1 :(得分:5)
建立我自己的控制。完成工作:)代码here
答案 2 :(得分:3)
你不需要自己构建任何东西只是用户安卓动画它会为你做的事情
用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()
}
}
})
答案 3 :(得分:1)
这对我来说很好,非常简单直接。
if (currentPosition > 0) {
ObjectAnimator animation = ObjectAnimator.ofInt(seekBar, "progress", currentPosition);
animation.setDuration(400);
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
}
答案 4 :(得分:0)
创建自定义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);
}
}
}
现在在XML中添加SeekBar并享受
<com.yourPackage.SmoothSeekBar
android:id="@+id/seekBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>