今天给大家分享一下小例子,就是SeekBar在移动时,当前进度也一起移动,具体看图啦。
其实原理很简单,就是在Seekbar在移动时,改变当前时间的位置。
代码如下:
public class MainActivity extends RxAppCompatActivity {
private TextView seekCurTime, curTime, totalTime;
private SeekBar seekBar;
//移动步长
private float moveStep;
private int screenWidth;
private static final int TOTALTIME = 300;//5分钟,单位秒
private int curtime;//单位秒
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
initData();
}
private void initView() {
seekCurTime = (TextView) findViewById(R.id.curSeekTime);
curTime = (TextView) findViewById(R.id.curTime);
totalTime = (TextView) findViewById(R.id.totalTime);
seekBar = (SeekBar) findViewById(R.id.seekbar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
curtime = progress * TOTALTIME / 100;
curTime.setText(Utils.getAudioTime(curtime));
seekCurTime.setText(Utils.getAudioTime(curtime));
setSeekCurTimeLocation(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
private void initData() {
curTime.setText(Utils.getAudioTime(0));
totalTime.setText(Utils.getAudioTime(TOTALTIME));
seekBar.setMax(100);
screenWidth = getWindowManager().getDefaultDisplay().getWidth() - 60;
moveStep = ((float) screenWidth / 100) * 1.0f;
Observable.
.interval(1, TimeUnit.SECONDS)
.compose(this.<Long>bindToLifecycle())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Long>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Long aLong) {
curtime++;
int progress = (int) ((float) curtime / TOTALTIME * 100);
seekBar.setProgress(progress);
curTime.setText(Utils.getAudioTime(curtime));
seekCurTime.setText(Utils.getAudioTime(curtime));
setSeekCurTimeLocation(progress);
}
});
}
private void setSeekCurTimeLocation(int progress) {
LinearLayout.LayoutParams layoutParams = (LinearLayout
.LayoutParams) seekCurTime.getLayoutParams();
int marginStart = (int) (progress * moveStep - seekCurTime.getWidth() / 2);
if (marginStart <= seekBar.getWidth() -seekCurTime.getWidth()) {
layoutParams.setMarginStart(marginStart);
}
seekCurTime.setLayoutParams(layoutParams);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
其他实现方式:Android使用SeekBar时动态显示进度且随SeekBar一起移动
layout()方法我在使用时无效,所以换成修改LayoutParams了。
补充:
计算实时进度的marginLeft,还可以通过SeekBar的Thumb的位置来计算,相对更容易些。代码如下:
private void setSeekProgressLocation() {
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) seekProgressTv.getLayoutParams();
float thumbWidth = sb_progress.getThumb().getBounds().width();
float thumbLeft = sb_progress.getThumb().getBounds().left;
int marginStart = (int) (sb_progress.getLeft() + thumbLeft + thumbWidth / 2 - layoutParams.width / 2);
layoutParams.setMarginStart(marginStart);
seekProgressTv.setLayoutParams(layoutParams);
上述代码中sb_progress为seekbar,seekProgressTv为实时进度的TextView。