Android仿虾米音乐播放器之自定义进度条seekbar

来源:互联网 发布:空燃比传感器数据 编辑:程序博客网 时间:2024/04/29 19:28

      先上图吧,仿照写的进度条


很明显不是系统的自带的进度条,所以我们需要自定义来实现这个效果,先看看官方给的例子

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item android:id="@android:id/background">        <shape>            <corners android:radius="5dip" />            <gradient                    android:startColor="#ff9d9e9d"                    android:centerColor="#ff5a5d5a"                    android:centerY="0.75"                    android:endColor="#ff747674"                    android:angle="270"            />        </shape>    </item>    <item android:id="@android:id/secondaryProgress">        <clip>            <shape>                <corners android:radius="5dip" />                <gradient                        android:startColor="#80ffd300"                        android:centerColor="#80ffb600"                        android:centerY="0.75"                        android:endColor="#a0ffcb00"                        android:angle="270"                />            </shape>        </clip>    </item>    <item android:id="@android:id/progress">        <clip>            <shape>                <corners android:radius="5dip" />                <gradient                        android:startColor="#ffffd300"                        android:centerColor="#ffffb600"                        android:centerY="0.75"                        android:endColor="#ffffcb00"                        android:angle="270"                />            </shape>        </clip>    </item></layer-list>

一个layer-list中包含有三个item,id分别是background,secondaryProgress,progress,分别对应的是背景,缓存进度条(播放视频的时候一般会有个灰色的进度条就是缓存的),自身的进度条。

为了自定义我们仿照上面的格式自己写一个,因为不需要缓存条,我们将中间的去掉,只写两个id

res/drawable/seek_horizontal

<?xml version="1.0" encoding="UTF-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android" >    <item        android:id="@android:id/background"        android:drawable="@drawable/equ_circle_bg"/>    <item android:id="@android:id/progress">        <clip android:drawable="@drawable/equ_circle_bg_c" >        </clip>    </item></layer-list>

两个drawable都是.9文件,一个是灰色,一个是橙色,写完之后我们就能在seekbar中使用了

 <SeekBar        android:id="@+id/seekBar1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:max="100"        android:maxHeight="1dp"        android:minHeight="1dp"        android:padding="0dp"        android:progress="0"        android:progressDrawable="@drawable/seek_horizontal"        android:thumb="@drawable/iconfont_yuan" />

     想要设置宽度的话需要同时设置maxHeight和minHeight,只设置一个是不会生效的,padding是设置seekbar距离两边的间隙,默认是有间隙的,thumb则是进度条上的刻度,写完布局后就能在代码中设置了。

    先继承OnSeekBarChangeListener接口并实现方法

@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {// Log.i("---" + Tag, "" + MusicService.getCurrent());// Log.i("---" + Tag, "" + MusicService.getDuration());}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {}@Overridepublic 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);}

分别是进度条改变,开始移动进度条,停止移动进度条。上面的发送广播是在进度条调整完后需要将进度发送给service,让MediaPlayer改变当前播放的进度。

  除此之外我们还要新开一个线程不断的更新进度条和播放的时间

class LooperThread extends Thread {@Overridepublic void run() {while (isTrue) {try {handler2.sendMessage(new Message());Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}}

设置sleep为1秒,就是每隔一秒更新一次进度条和播放进度

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);}};};

先判断播放的状态,只有播放的时候才会更新,除此之外在返回时需要将进程停止掉,不然占有线程,也无法释放内存,开始试了一下普通的方法直接调用stop方法,发现还是会执行,只有给一个状态值,让run方法不再执行就可以了。

@Overridepublic void onBackPressed() {super.onBackPressed();// 按返回键时将isTrue设为false,让线程不再继续isTrue = false;finish();}





0 0
原创粉丝点击