本文介绍了在确定模式如何使用Android的进度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写一个媒体播放器,我想有一个进度条显示歌曲的进度。我找到了ProgressBar类,但我可以在屏幕上得到的是一个圆形旋转图标。什么即时寻找的是一个实际的吧。
I am writing a media player and i would like to have a progress bar showing the progress of the song. I found the ProgressBar class, but all i can get on the screen is a circular spinning icon. what im looking for is an actual bar.
我要如何改变进度的风格了吧(不是圆形),以及如何,我会使用它的MediaPlayer?
How do i change the style of the ProgressBar to be a bar (not a circle) and how would i use it with MediaPlayer?
感谢
推荐答案
使用的样式安卓?ATTR / progressBarStyleHorizontal
例如:
<ProgressBar android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
这是MediaPlayer的一个例子:
and this is an example with MediaPlayer:
package com.playerpgbar;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class Player extends Activity implements Runnable, OnClickListener {
private TextView status;
private ProgressBar progressBar;
private Button startMedia;
private Button stop;
private MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
status = (TextView) findViewById(R.id.status);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
startMedia = (Button) findViewById(R.id.startMedia);
stop = (Button) findViewById(R.id.stop);
startMedia.setOnClickListener(this);
stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.equals(startMedia)) {
if (mp != null && mp.isPlaying()) return;
mp = MediaPlayer.create(Player.this, R.raw.exodus_piranha);
mp.start();
status.setText(R.string.PlayingMedia);
progressBar.setVisibility(ProgressBar.VISIBLE);
progressBar.setProgress(0);
progressBar.setMax(mp.getDuration());
new Thread(this).start();
}
if (v.equals(stop) && mp!=null) {
mp.stop();
mp = null;
status.setText(R.string.Stopped);
progressBar.setVisibility(ProgressBar.GONE);
}
}
@Override
public void run() {
int currentPosition= 0;
int total = mp.getDuration();
while (mp!=null && currentPosition<total) {
try {
Thread.sleep(1000);
currentPosition= mp.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
progressBar.setProgress(CurrentPosition);
}
}
}
这篇关于在确定模式如何使用Android的进度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!