我之前有写过一篇“Android之进度条1”,那个是条形的进度条(显示数字进度),这次实现圆形进度条。
点击查看Android之进度条1:http://www.cnblogs.com/caidupingblogs/p/5102745.html>
MainActivity继承Activity实现Runnable接口:
package com.cdp.progressbar; import android.app.Activity; import android.os.Bundle; import android.widget.ProgressBar; //使用Runnable接口 public class MainActivity extends Activity implements Runnable { //声明一条线程 private Thread th ; //声明一个进度条对象 private ProgressBar pb ; //标识进度值最大最小的状态 private boolean stateChage; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //实例进度条对象 pb = (ProgressBar)findViewById(R.id.porb); //实例线程对象 th = new Thread(this); //启动线程 th.start(); } @Override public void run() {//实现Runnable接口抽象函数 while(true){ //得到当前进度值 int current = pb.getProgress(); //得到进度条的最大进度值 int currentMax = pb.getMax(); //得到底层当前进度值 int secCurrent = pb.getSecondaryProgress(); //以下代码实现进度值越来越大,越来越小的一个动态效果 if(stateChage==false){ if(current>=currentMax){ stateChage=true; }else{ //设置进度值 pb.setProgress(current+1); //设置底层进度值 pb.setSecondaryProgress(current+1); } }else{ if(current<=0){ stateChage=false; }else{ pb.setProgress(current-1); pb.setSecondaryProgress(current-1); } } try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
run:
时间: 2024-10-22 20:14:19