在听网络音乐或者下载软件的时候总能看到进度条的身影 它是怎么实现的呢

应该是已下载的大小/总的应用的大小  哈哈 

progress小例子

android:progressbar实现进度条_ide

android:progressbar实现进度条_ide_02

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加" />

<Button
android:id="@+id/sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="减少" />

<Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重置" />
<Button
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示进度条对话框" />

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:progress="20"
android:secondaryProgress="30" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />

</LinearLayout>



代码很简单

就不说了  主要的都有注释

package com.example.progressbar;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

private Button button1;
private Button button2;
private Button button3;
private Button show;
private TextView textView;
private ProgressBar progress, progress1;
private ProgressDialog progressDialog;
private int max;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* 启用窗口特征,启用两种形式进度的进度条
*/
requestWindowFeature(Window.FEATURE_PROGRESS);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
/*
* 显示两种进度
*/
//进度可见
setProgressBarVisibility(true);
//明确显示进度的
setProgressBarIndeterminate(false);
//最大进度为10000
setProgress(3000);
//进度可见
setProgressBarIndeterminateVisibility(true);

button1 = (Button) findViewById(R.id.add);
button2 = (Button) findViewById(R.id.sub);
button3 = (Button) findViewById(R.id.reset);
show = (Button) findViewById(R.id.show);
textView = (TextView) findViewById(R.id.textView1);
progress = (ProgressBar) findViewById(R.id.progressBar);
init();
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
show.setOnClickListener(this);

}
//初始化
private void init() {
// TODO Auto-generated method stub
max = progress.getMax();
progress.setProgress(50);
progress.setSecondaryProgress(60);
textView.setText("第一进度的百分比为" + (int)((float) progress.getProgress()
/ (float) max *100)+ "%,第二进度条的百分比为"
+ (int)((float) progress.getSecondaryProgress() / (float) max *100)+ "%");
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
//点击增加按钮 通过incrementProgressBy方法 增加进度
case R.id.add:
progress.incrementProgressBy(10);
progress.incrementSecondaryProgressBy(10);
break;
//点击减少按钮 通过incrementProgressBy方法 减少进度
case R.id.sub:
progress.incrementProgressBy(-10);
progress.incrementSecondaryProgressBy(-10);
break;
case R.id.reset:
init();
break;
case R.id.show:
progressDialog=new ProgressDialog(MainActivity.this);
//设置对话框进度条的图标
progressDialog.setIcon(R.drawable.ic_launcher);
//设置对话框进度条的标题
progressDialog.setTitle("苏苏爱自由");
//设置对话框进度条的内容
progressDialog.setMessage("欢迎进入苏苏的博客");
//设置对话框进度条的显示风格
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//不是明确显示进度的
progressDialog.setIndeterminate(true);
//添加一个确定按钮 并为其添加事件
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "谢谢大家的支持", Toast.LENGTH_SHORT).show();
}
});
//显示对话框进度条
progressDialog.show();
break;
}
textView.setText("第一进度的百分比为" + (int)((float) progress.getProgress()
/ (float) max *100)+ "%,第二进度条的百分比为"
+ (int)((float) progress.getSecondaryProgress() / (float) max *100)+ "%");
}

}