直播app系统源码,Android的进度条与拖动条
作者:互联网
直播app系统源码,Android的进度条与拖动条
一、进度条
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到控件
final ProgressBar bigProgressBar = (ProgressBar) findViewById(R.id.bigprogressBar);
final ProgressBar smallProgressBar = (ProgressBar) findViewById(R.id.smallprogressBar);
final TextView tv_progress1 = (TextView) findViewById(R.id.tv_progress1);
final TextView tv_progress2 = (TextView) findViewById(R.id.tv_progress2);
// 匿名内部类实现按钮点击事件,开始加载
findViewById(R.id.btn_load).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
start(bigProgressBar, tv_progress1);
start(smallProgressBar, tv_progress2);
}
});
}
// 进度条开始变化的方法
public void start(final ProgressBar progressBar, final TextView textView) {
// 耗时任务放在子线程种进行
new Thread() {
private int nowProgress;
private int maxProgress;
public void run() {
// 得到进度条当前的值
nowProgress = progressBar.getProgress();
// 得到进度条最大值
maxProgress = progressBar.getMax();
// 当当前进度小于最大进度值时
while (nowProgress < maxProgress) {
nowProgress++;
progressBar.setProgress(nowProgress);
// 表示在UI线程种更新TextView因为子线程不能更新UI
runOnUiThread(new Runnable() {
@Override
public void run() {
// 设置TextView的内容
textView.setText(nowProgress + "/" + maxProgress);
}
});
try {
// 延时模拟加载进度
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}.start();
}
}
二、拖动条
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到SeekBar
SeekBar seekBar = (SeekBar) findViewById(R.id.seekbar);
// 获取音量管理器
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
// 获取当前音乐音量的值 STREAM_MUSIC
int index = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
// 获取音乐音量的最大值
int max = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
// 设置拖动条当前值
seekBar.setProgress(index);
// 设置拖动条的最大值
seekBar.setMax(max);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// 拖动停止时调用
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// 拖动开始时调用
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// 拖动改变时调用
// 获取seeKbar的当前值
int seekBarvalue = seekBar.getProgress();
// 设置音量大小,并在UI上显示 AudioManager.FLAG_SHOW_UI
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, seekBarvalue, AudioManager.FLAG_SHOW_UI);
}
});
}
}
以上就是 直播app系统源码,Android的进度条与拖动条,更多内容欢迎关注之后的文章
标签:进度条,拖动,app,seekBar,源码,tv,void,TextView 来源: https://www.cnblogs.com/yunbaomengnan/p/16517233.html