Android学习:SeekBar实现音量调节
2012-04-10 22:26
411 查看
Android学习:SeekBar实现音量调节
2011-11-28 14:42412人阅读评论(0)收藏举报SeekBar可以通过滑块的位置来标识数值----而且拖动条允许用户拖动滑块来改变值,因此拖动条通常用于对系统的某种数值进行调节,比如调节音量等。
SeekBar允许用户改变拖动条的滑块外观,改变滑块外观通常通过如下属性来指定: android:thumb: 指定一个Drawable对象,该对象将自定义滑块。
为了让程序能响应拖动条滑块位置的改变,程序可以考虑为它绑定一个OnSeekBarChangeListener监听器。
以下是一个使用SeekBar来调节系统音量的实例:
XML代码:
[html] view plaincopyprint?
<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"/> //滑块样式
[html] view plaincopyprint? <?xmlversion="1.0"encoding="UTF-8"?> <layer-listxmlns:android="http://schemas.android.com/apk/res/android"> <itemandroid:id="@android:id/background"> <shape> <cornersandroid:radius="10dip"/> <gradientandroid:startColor="#ffffffff" android:centerColor="#ff000000"android:endColor="#ff808A87" android:centerY="1"android:angle="270"/> </shape> </item> <itemandroid:id="@android:id/progress"> <clip> <shape> <cornersandroid:radius="10dip"/> <gradientandroid:startColor="#ffffffff" android:centerColor="#ffFFFF00"android:endColor="#ffAABD00" android:centerY="1"android:angle="270"/> </shape> </clip> </item> </layer-list> <?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 plaincopyprint?
<?xmlversion="1.0"encoding="UTF-8"?>
<selectorxmlns: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>
[html] view plaincopyprint? <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> <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 plaincopyprint?
publicclass 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;
privateint maxVolume, currentVolume;
privateint volume=0; //初始化声音
@Override
publicvoid 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() //调音监听器
{
publicvoid 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
publicvoid onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
publicvoid onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
相关文章推荐
- (转)Android学习:SeekBar实现音量调节
- Android学习:SeekBar实现音量调节
- Android学习:SeekBar实现音量调节
- Android学习:SeekBar实现音量调节
- Android 自定义SeekBar动态改变 硬件音量大小 实现和音量键的同步
- Android 自定义SeekBar动态改变硬件音量大小实现和音量键的同步(推荐)
- Android学习:SeekBar实现音量调节
- Android移动开发-音量调节对话框的实现
- SeekBar------实现调节音量的控件
- Android 使用SeekBar调节系统音量
- Android学习:SeekBar实现音量调节
- 【Android】Android实现视频播放的时候滑动屏幕调节亮度和音量
- Android 利用seekbar结合AudioManager调节系统音量
- SeekBar------实现调节音量的控件
- Android实现音量调节的方法
- Android Framework学习笔记 -- Audio调节音量流程
- 如何实现Android音量seekbar
- Android实现音量调节的方法
- 【Android】Android开发实现进度条效果,SeekBar的简单使用。音量,音乐播放进度,视频播放进度等
- Android学习:SeekBar实现音量调节