private ProgressBar horizonBar;
private ProgressBar circleBar;
private int mProgressStatus;//显示的进度值
private Handler mHandler;//声明一个处理消息的Handler类对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
horizonBar = (ProgressBar)findViewById(R.id.progressBar1);
circleBar = (ProgressBar)findViewById(R.id.progressBar2);
horizonBar.incrementProgressBy(-10);
horizonBar.setVisibility(View.VISIBLE);
mHandler = new Handler(){
@Override
public void handleMessage(Message msg){
if(msg.what == 0x111){
horizonBar.setProgress(mProgressStatus);
circleBar.setProgress(100-mProgressStatus);
}else{
Toast.makeText(MainActivity.this, "耗时已经完成", Toast.LENGTH_SHORT).show();
horizonBar.setVisibility(View.GONE);
circleBar.setVisibility(View.GONE);
}
}
};
new Thread(new Runnable() {
@Override
public void run() {
while(true){
mProgressStatus = doWork();// TODO Auto-generated method stub
Message msg = new Message();
if(mProgressStatus < 100){
msg.what = 0x111;
mHandler.sendMessage(msg);
}else{
msg.what = 0x110;
mHandler.sendMessage(msg);
}
}
}
private int doWork(){
mProgressStatus+=Math.random()*10;
try{
Thread.sleep(200);
}catch(InterruptedException e){
e.printStackTrace();
}
return mProgressStatus;
}
}).start();
}
效果如下: