Android画布中的倒数计时器

时间:2018-07-12 05:11:30

标签: android canvas

我想在用户参加活动时显示倒数计时器。该活动是使用画布设计的。

所以我用这段代码在活动中显示了计时器。

private void drawTimer(){
        paint.setTextSize(10);
        paint.setTextAlign(Paint.Align.CENTER);

        new CountDownTimer(240000, 1000) {

            public void onTick(long millisUntilFinished) {
                int seconds = (int) ((millisUntilFinished/1000)%60);
                int minutes = (int) ((millisUntilFinished/1000)/60);

                String startTime =String.format("%02d:%02d",minutes,seconds);
                canvas1.drawText(startTime,
                        200, 400, paint);
                Log.d("ShowTimer",startTime);
            }

            public void onFinish() {
                canvas1.drawText("done!",
                        200, 400, paint);
                Log.d("ShowTimer","done!");
            }
        }.start();
    }

在日志文件中正确显示倒计时。但是在画布上,它仅显示03:59,此后该值未更改。如果有人可以提出要求的更改,问题出在哪里?谢谢。

通过在画布中动态使用TextView对其进行了更改-

layout = new LinearLayout(context1);
        textView = new TextView(context1);
        textView.setVisibility(View.VISIBLE);
        textView.setText("Hello world");
        layout.addView(textView);

        layout.measure(canvas.getWidth(), canvas.getHeight());
        layout.layout(0, 0, canvas.getWidth(), canvas.getHeight());
        layout.draw(canvas);

        new CountDownTimer(240000, 1000) {

            public void onTick(long millisUntilFinished) {
                int seconds = (int) ((millisUntilFinished/1000)%60);
                int minutes = (int) ((millisUntilFinished/1000)/60);

                String startTime =String.format("%02d:%02d",minutes,seconds);
                textView.setText(startTime);
                Log.d("ShowTimer",startTime);
            }

            public void onFinish() {
                textView.setText("done!");
                Log.d("ShowTimer","done!");
            }
        }.start();

然后,同样的问题,在加载活动“ Hello World”时显示,但未显示计时器,而是将其显示在日志文件中。

1 个答案:

答案 0 :(得分:0)

function regExIndexOf(array: string[], term: RegExp, start = 0): number {
  let result = -1;
  array.forEach((item, index) => {
    if (result === -1 && index >= start && term.test(item)) {
      result = index;
    }
  });
  return result;
}

regExIndexOf(["Title", "First", "Last", "Email", "↵"], new RegExp('Title')); // 0
regExIndexOf(["Title", "First", "Last", "Email", "↵"], new RegExp('Title'), 1); // -1
regExIndexOf(["Title", "First", "Last", "Email", "↵"], new RegExp('\u{21b5}', 'u')); //4

}