您的位置:首页 > 移动开发 > Android开发

6.Android基础:常见控件----->SeekBar

2016-09-13 17:05 387 查看
SeekBar控件用于改变进度条位置,允许拖动滑块来改变当前值



重要属性

android:thumb

指定滑动块的显示样式为一个Drawable

[b]重要方法
[/b]

[b]setMax[/b]

功能:设置进度最大值

参数:整数

[b]setProgress[/b]

功能:设置当前进度值

参数:整数

监听器

setOnSeekBarChangeListener

监听滑动位置

下面举个简单例子

这个是布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>

<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/textview"
/>

</LinearLayout>
这个是测试类,用Handler类实时更新进度条位置

public class MainActivity extends Activity {

private SeekBar seekbar;
private TextView textview;

//刷新子线程
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
//得到当前进度条
textview.setText((long)seekbar.getProgress() * 100 / seekbar.getMax() + "%");
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

seekbar = (SeekBar) findViewById(R.id.seekbar);
textview = (TextView) findViewById(R.id.textview);
//设置进度条最大值
seekbar.setMax(100);
//监听进度条
seekbar.setOnSeekBarChangeListener(new SeekBarListener());
}

//定义一个监听器,监听进度条状态的改变
private class SeekBarListener implements SeekBar.OnSeekBarChangeListener {

//当进度条发生变换时调用
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
System.out.println(progress);
//发送消息,让文本进度条改变
handler.sendEmptyMessage(1);
}

//当用户开始滑动时调用
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
System.out.println("开始滑动" + seekBar.getProgress());

}

//当用户结束滑动时调用
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
System.out.println("结束滑动" + seekBar.getProgress());
}

}

}


下面做一个简单应用音乐播放应用,采用获取服务器资源,播放音乐,里面也有在内存中读取音乐

这个是一个布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放"
android:onClick="play"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"
android:onClick="pause"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="继续播放"
android:onClick="continuePlay"/>

<SeekBar
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

</LinearLayout>
首先,先注册一个音乐服务,,用作播放,暂停,添加以下代码

 <service android:name="com.musicplayer.MusicService"></service>

然后创建三个类,一个接口类ControllerInterface,一个操作类,一个测试类

接口类:主要把需要实现的功能,抽取出来,方便操作类使用

public interface ControllerInterface {
void play();
void pause();
void continuePaly();
void seekTo(int progress);
}
操作类:具有操作音乐进程,拖动,暂停,播放
public class MusicService extends Service {

MediaPlayer player;
private Timer timer;

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
//返回调用里面的方法内容
return new MusicConntroller();
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
player = new MediaPlayer();
}

//退出程序,应用停止
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//销毁player
player.start();
player.release();
if(timer != null) {
timer.cancel();
timer = null;
}
}

//控制音乐,调用服务方法
class MusicConntroller extends Binder implements ControllerInterface {

@Override
public void play() {
// TODO Auto-generated method stub
MusicService.this.play();
}

@Override
public void pause() {
// TODO Auto-generated method stub
MusicService.this.pause();
}

@Override
public void continuePaly() {
// TODO Auto-generated method stub
MusicService.this.continuePaly();
}

@Override
public void seekTo(int progress) {
// TODO Auto-generated method stub
MusicService.this.seekTo(progress);
}

}

public void play() {
// System.out.println("开始播放音乐");
//进入空闲状态
player.reset();
try {
//资源路径
// player.setDataSource("sdcard/jw.mp3"); //这种方式放在内存中
player.setDataSource("http://11.1.42.9:8080/jw.mp3"); //这种方式放在服务中
//准备播放状态(同步准备)
// player.prepare();
//异步准备
player.prepareAsync();
//检测准备完毕再开始播放音乐
player.setOnPreparedListener(new OnPreparedListener() {

@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
//开始播放
player.start();
addTimer();
}

});

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void pause() {
System.out.println("暂停播放音乐");
//暂停播放音乐
player.pause();
}

public void continuePaly() {
System.out.println("继续播放音乐");
//暂停播放
player.start();
}

public void seekTo(int progress) {
player.seekTo(progress);
}

//获取音乐时长
public void addTimer() {
if(timer == null) {
timer = new Timer();
timer.schedule(new TimerTask() {

//5毫秒开始执行这个子线程,并且每500毫秒执行一次
//这个run方法在子线程执行
@Override
public void run() {
// TODO Auto-generated method stub
//获取播放总事长
int duration = player.getDuration();
//获取当前播放进度
int currentPosition = player.getCurrentPosition();

//发送消息
Message msg = MainActivity.handler.obtainMessage();
//把发送数据封装至消息对象中
Bundle data = new Bundle();
//把数据添加Bundle对象中
data.putInt("duration", duration);
data.putInt("currentPosition", currentPosition);
msg.setData(data);

//发送数据
MainActivity.handler.sendMessage(msg);
}
//计时任务开始5毫秒后,run方法执行,每500毫秒执行一次
}, 5, 500);
}
}
}测试类:侦听用户操作,返回消息,做出反应
public class MainActivity extends Activity {

ControllerInterface ci;
private static SeekBar sb;

public static Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
//拿到Bundle对象
Bundle bundle = msg.getData();
//取出消息内容
int duration = bundle.getInt("duration");
int currentPosition = bundle.getInt("currentPosition");

//设置最大值
sb.setMax(duration);
//设置进度值
sb.setProgress(currentPosition);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

sb = (SeekBar) findViewById(R.id.sb);

//设置滑动侦听
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

//seekBar:触发该方法执行的seekBar对象
//手指抬起
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
System.out.println("手指抬起");
//获取sb的当前进度,然后设置给音乐服务的播放进度
ci.seekTo(seekBar.getProgress());
}

//手指按下
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
System.out.println("手指按下");
}

//手指滑动
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
System.out.println("手指滑动");
}
});

Intent intent = new Intent(this, MusicService.class);
//开始服务,防止返回被自动解绑服务,音乐暂停(把进程变成服务进程)
startService(intent);
//绑定服务(获取中间对象)
bindService(intent, new ServiceConnection() {

//到服务的连接建立了,此方法调用
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
ci = (ControllerInterface) service;
}

//到服务的连接中断了,此方法调用
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub

}

}, BIND_AUTO_CREATE);
}

public void play(View v) {
ci.play();
}

public void pause(View v) {
ci.pause();
}

public void continuePlay(View v) {
ci.continuePaly();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: