当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Circle progress bar with countdown

问题描述:

I have the following two programs progress, countDownTimer that works but need to combine them to achieve what I want.Please see the image below.

I have the following issues.

  1. Progress bar not smooth

  2. Countdown time is going up and down and not counting down correctly

    public class MainActivity extends ActionBarActivity {

    TextView tv2;

    ProgressBar pBar2;

    int pStatus = 0;

    private Handler handler = new Handler();

    @Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    tv2 = (TextView) findViewById(R.id.textView2);

    pBar2 = (ProgressBar) findViewById(R.id.progressBar2);

    new Thread(new Runnable() {

    @Override

    public void run() {

    // TODO Auto-generated method stub

    while (pStatus < 100) {

    pStatus += 1;

    handler.post(new Runnable() {

    @Override

    public void run() {

    new CountDownTimer(10000, 1000) {

    @Override

    public void onTick(long millisUntilFinished) {

    pBar2.setProgress(pStatus);

    tv2.setText("" + millisUntilFinished / 1000);

    }

    @Override

    public void onFinish() {

    tv2.setText("Go!");

    }

    }.start();

    }

    });

    try {

    // Sleep for 200 milliseconds.

    // Just to display the progress slowly

    Thread.sleep(200);

    } catch (InterruptedException e) {

    e.printStackTrace();

    }

    }

    }

    }).start();

    }

网友答案:

I once encountered a similar problem while developing a soccer Application and ended up writing my own View and overwriting onDraw and drawing Circles (Arcs) and writing a Text.

Going for this approach makes it much easier to control the View and not abuse ProgressBar to draw the Circle around - which even makes the View more lightweight.

In the right you can see the clock and the circles around drawing the state of the game.

So just go for a custom View and overrite the onDraw and manipulate the Canvas.

Good point to start learning this is here:

http://developer.android.com/training/custom-views/index.html

分享给朋友:
您可能感兴趣的文章:
随机阅读: