Android——SeekBar动态显示进度

来源:互联网 发布:手机在淘宝怎么退换货 编辑:程序博客网 时间:2024/05/16 09:18

今天给大家分享一下小例子,就是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了。

阅读全文
0 0
原创粉丝点击