先看自定义的seekbar
<SeekBar
android:id="@+id/soundseekbar"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:maxHeight="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:progressDrawable="@drawable/voicebarbg"
android:thumb="@drawable/voiceslid" />
maxHeight可自己调节,不设这个属性seekbar有可能特别粗,达不到想要的效果。
thumb就是滑块的图片,progressDrawable是背景图片
voicebarbg.xml
<?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/voicebg">
</item>
<item android:id="@android:id/secondaryProgress"
android:drawable="@drawable/voiceprogress">
</item>
<item android:id="@android:id/progress"
android:drawable="@drawable/voiceprogress">
</item>
</layer-list>
background是背景
![](https://img-my.csdn.net/uploads/201212/04/1354615337_8251.png)
![](https://img-my.csdn.net/uploads/201212/04/1354615327_1958.png)
也可以参考一下这个,自己定义颜色的
<?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/progressbar">
</item>
<item android:id="@android:id/secondaryProgress"
>
<clip>
<shape>
<corners android:radius="0dip" />
<gradient
android:startColor="#800096ff"
android:centerColor="#800096ff"
android:centerY="0.75"
android:endColor="#a00096ff"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress"
>
<clip>
<shape>
<corners android:radius="0dip" />
<gradient
android:startColor="#ff0096ff"
android:centerColor="#ff0096ff"
android:centerY="0.75"
android:endColor="#ff0096ff"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>
嗯这样一个自定义的seekbar就出来了。下面说说seekbar控制声音,获取系统声音管理器
AudioManager mAudioManager = (AudioManager) PlayVideoFullScreenAct.this.getSystemService(Activity.AUDIO_SERVICE);
最大声音值
int maxSound = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
当前声音值
curSound = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
把最大声音值和当前声音值传给seekbar
soundbar=(SeekBar)findViewById(R.id.soundseekbar);
soundbar.setMax(maxSound);
soundbar.setProgress(curSound);
然后写个seekbar的监听事件就ok了
soundbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser){
curSound = seekBar.getProgress();
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
curSound, 0);
}
}
});