(转)Android学习:SeekBar实现音量调节
2014-03-08 11:16
567 查看
SeekBar可以通过滑块的位置来标识数值----而且拖动条允许用户拖动滑块来改变值,因此拖动条通常用于对系统的某种数值进行调节,比如调节音量等。
SeekBar允许用户改变拖动条的滑块外观,改变滑块外观通常通过如下属性来指定: android:thumb: 指定一个Drawable对象,该对象将自定义滑块。
为了让程序能响应拖动条滑块位置的改变,程序可以考虑为它绑定一个OnSeekBarChangeListener监听器。
以下是一个使用SeekBar来调节系统音量的实例:
XML代码:
[html] view
plaincopy
<SeekBar
android:id="@+id/sound"
android:layout_width="150px"
android:layout_height="10px"
android:max="100" //设置拖动条最大值
android:progress="10" //设置拖动条当前值
android:progressDrawable="@layout/seekbar_style" //拖动条样式
android:thumb="@layout/thumb" /> //滑块样式
seekbar_style.xml:
[html] view
plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="10dip" />
<gradient android:startColor="#ffffffff"
android:centerColor="#ff000000" android:endColor="#ff808A87"
android:centerY="1" android:angle="270" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="10dip" />
<gradient android:startColor="#ffffffff"
android:centerColor="#ffFFFF00" android:endColor="#ffAABD00"
android:centerY="1" android:angle="270" />
</shape>
</clip>
</item>
</layer-list>
thumb.xml:
[html] view
plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 按下状态 -->
<item
android:state_pressed="true"
android:drawable="@drawable/thumb_normal"
/>
<!-- 普通无焦点状态 -->
<item
android:state_focused="false"
android:state_pressed="false"
android:drawable="@drawable/thumb_normal"
/>
</selector>
bacon_seekbar.xml:
[html] view
plaincopy
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="@+android:id/background"
android:drawable="@drawable/thumb_normal" />
<item
android:id="@+android:id/SecondaryProgress"
android:drawable="@drawable/thumb_normal" />
<item
android:id="@+android:id/progress"
android:drawable="@drawable/thumb_normal" />
</layer-list>
JAVA代码:
[java] view
plaincopy
public class PianoActivity extends Activity {
/** Called when the activity is first created. */
private ImageButton imageButton_white1;
private MediaPlayer mediaPlayer01;
public AudioManager audiomanage;
private TextView mVolume ; //显示当前音量
public SeekBar soundBar;
private int maxVolume, currentVolume;
private int volume=0; //初始化声音
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mediaPlayer01 = new MediaPlayer();
imageButton_white1=(ImageButton)findViewById(R.id.white1);
final SeekBar soundBar=(SeekBar)findViewById(R.id.sound); //音量设置
mVolume = (TextView)findViewById(R.id.mVolume);
audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
maxVolume = audiomanage.getStreamMaxVolume(AudioManager.STREAM_MUSIC); //获取系统最大音量
soundBar.setMax(maxVolume); //拖动条最高值与系统最大声匹配
currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC); //获取当前值
soundBar.setProgress(currentVolume);
mVolume.setText(currentVolume*100/maxVolume + " %");
soundBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() //调音监听器
{
public void onProgressChanged(SeekBar arg0,int progress,boolean fromUser)
{
audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC); //获取当前值
soundBar.setProgress(currentVolume);
mVolume.setText(currentVolume*100/maxVolume + " %");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
TextView的XML没有给出,需要自己添加。
完成效果:
更多1
SeekBar允许用户改变拖动条的滑块外观,改变滑块外观通常通过如下属性来指定: android:thumb: 指定一个Drawable对象,该对象将自定义滑块。
为了让程序能响应拖动条滑块位置的改变,程序可以考虑为它绑定一个OnSeekBarChangeListener监听器。
以下是一个使用SeekBar来调节系统音量的实例:
XML代码:
[html] view
plaincopy
<SeekBar
android:id="@+id/sound"
android:layout_width="150px"
android:layout_height="10px"
android:max="100" //设置拖动条最大值
android:progress="10" //设置拖动条当前值
android:progressDrawable="@layout/seekbar_style" //拖动条样式
android:thumb="@layout/thumb" /> //滑块样式
seekbar_style.xml:
[html] view
plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="10dip" />
<gradient android:startColor="#ffffffff"
android:centerColor="#ff000000" android:endColor="#ff808A87"
android:centerY="1" android:angle="270" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="10dip" />
<gradient android:startColor="#ffffffff"
android:centerColor="#ffFFFF00" android:endColor="#ffAABD00"
android:centerY="1" android:angle="270" />
</shape>
</clip>
</item>
</layer-list>
thumb.xml:
[html] view
plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 按下状态 -->
<item
android:state_pressed="true"
android:drawable="@drawable/thumb_normal"
/>
<!-- 普通无焦点状态 -->
<item
android:state_focused="false"
android:state_pressed="false"
android:drawable="@drawable/thumb_normal"
/>
</selector>
bacon_seekbar.xml:
[html] view
plaincopy
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="@+android:id/background"
android:drawable="@drawable/thumb_normal" />
<item
android:id="@+android:id/SecondaryProgress"
android:drawable="@drawable/thumb_normal" />
<item
android:id="@+android:id/progress"
android:drawable="@drawable/thumb_normal" />
</layer-list>
JAVA代码:
[java] view
plaincopy
public class PianoActivity extends Activity {
/** Called when the activity is first created. */
private ImageButton imageButton_white1;
private MediaPlayer mediaPlayer01;
public AudioManager audiomanage;
private TextView mVolume ; //显示当前音量
public SeekBar soundBar;
private int maxVolume, currentVolume;
private int volume=0; //初始化声音
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mediaPlayer01 = new MediaPlayer();
imageButton_white1=(ImageButton)findViewById(R.id.white1);
final SeekBar soundBar=(SeekBar)findViewById(R.id.sound); //音量设置
mVolume = (TextView)findViewById(R.id.mVolume);
audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
maxVolume = audiomanage.getStreamMaxVolume(AudioManager.STREAM_MUSIC); //获取系统最大音量
soundBar.setMax(maxVolume); //拖动条最高值与系统最大声匹配
currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC); //获取当前值
soundBar.setProgress(currentVolume);
mVolume.setText(currentVolume*100/maxVolume + " %");
soundBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() //调音监听器
{
public void onProgressChanged(SeekBar arg0,int progress,boolean fromUser)
{
audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC); //获取当前值
soundBar.setProgress(currentVolume);
mVolume.setText(currentVolume*100/maxVolume + " %");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
TextView的XML没有给出,需要自己添加。
完成效果:
更多1
相关文章推荐
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- 解決Linux下Android开发真机调试设备不被识别问题
- [Android]在代码里运行另一个程序的方法
- [软件咨询]WPS2012正式版已发布 金山Office移动版4.0发布
- Android笔记-Linux Kernel Ftrace (Function Trace)解析
- android USB如何修改VID具体实现
- Android增量升级的方法和原理详细介绍
- Android Mouse实现过程详细笔记
- 深入Android Browser配置管理的详解
- Android Mms之:深入理解对话列表管理
- Android APP与媒体存储服务的交互
- android 多线程技术应用
- Android之采用execSQL与rawQuery方法完成数据的添删改查操作详解
- Android数据类型之间相互转换系统介绍
- 通过Android trace文件分析死锁ANR实例过程
- Android实现左右滑动效果的方法详解
- Android 各国语言缩写及简称详细介绍
- Android 自动判断是电话,网址,EMAIL方法之Linkify的使用