先上图吧,仿照写的进度条
很明显不是系统的自带的进度条,所以我们需要自定义来实现这个效果,先看看官方给的例子
-
-
-
为了自定义我们仿照上面的格式自己写一个,因为不需要缓存条,我们将中间的去掉,只写两个id
res/drawable/seek_horizontal
-
先继承OnSeekBarChangeListener接口并实现方法
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// Log.i("---" + Tag, "" + MusicService.getCurrent());
// Log.i("---" + Tag, "" + MusicService.getDuration());
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
Log.i("---" + Tag, "" + progress);
Intent intent = new Intent();
intent.setAction(Constants.ACTION_SEEK);
intent.putExtra("progress", progress);
sendBroadcast(intent);
}
除此之外我们还要新开一个线程不断的更新进度条和播放的时间
class LooperThread extends Thread {
@Override
public void run() {
while (isTrue) {
try {
handler2.sendMessage(new Message());
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private Handler handler2 = new Handler() {
public void handleMessage(Message msg) {
if (MusicService.isPlaying) {
long current = MusicService.getCurrent();
long duration = MusicService.getDuration();
String text_current = MediaUtil.formatTime(current);
mTexting.setText(text_current);
long a = 100L * current / duration;
int progress = new Long(a).intValue();
mSeekBar.setProgress(progress);
}
};
};
@Override
public void onBackPressed() {
super.onBackPressed();
// 按返回键时将isTrue设为false,让线程不再继续
isTrue = false;
finish();
}