ProgressBar 等比例
优点:任务完成 其也正好到达终点
缺点:需要知道以下变量取值:
1. ProgressBar.max()
2. 任务所需总长度
3. 更新时 任务的完成值
整个代码 或是 思路 都比较简单 直接上代码 有问题或其他 请跟帖
1. main.xml 2个ProgressBar
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ProgressBar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/progress1" android:max="100" style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingTop="100dip" /> <ProgressBar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/progress2" android:max="30" style="?android:attr/progressBarStyleHorizontal" android:layout_width="100dip" android:layout_height="wrap_content" android:paddingTop="100dip" /> </LinearLayout>
2. ProgressUsage.java
public class ProgressUsage extends Activity {
ProgressBar pBar1,pBar2;
Handler handler = new Handler();
MediaPlayer mp;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pBar1 = (ProgressBar)findViewById(R.id.progress1);
pBar2 = (ProgressBar)findViewById(R.id.progress2);
mp = new MediaPlayer();
AssetFileDescriptor fd = getResources().openRawResourceFd(R.raw.audio);
try {
mp.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
fd.close();
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
startProgressUpdate();
}
public void startProgressUpdate(){
Runnable update = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
int position = mp.getCurrentPosition();
int duration = mp.getDuration();
int max1 = pBar1.getMax();
int max2 = pBar2.getMax();
//等比例取值
int i1 = max1 * position/duration;
int i2 = max2 * position/duration;
pBar1.setProgress(i1);
pBar2.setProgress(i2);
startProgressUpdate();
}
};
handler.postDelayed(update, 10);
}
}
3. emulator 运行截图: 注意2ProgressBar 虽然刷新频率一样 但是刷新长度不等