简体   繁体   English

Android画布中的倒数计时器

[英]Countdown timer in canvas android

I want to display a countdown timer while the user is in an activity. 我想在用户处于活动状态时显示倒数计时器。 The activity is designed using a canvas. 该活动是使用画布设计的。

So i used this code to display the timer in the activity. 因此,我使用此代码来显示活动中的计时器。

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();
    }

While in the log file its showing the countdown correctly. 在日志文件中,其正确显示倒数计时。 But in canvas its showing only 03:59, after that the value is not getting changed. 但是在画布上,它仅显示03:59,此后该值未更改。 Where is the problem if anyone can suggest the required changes. 如果有人可以提出要求的更改,问题出在哪里? Thanks. 谢谢。

Changed it by using a TextView in the canvas dynamically - 通过在画布中动态使用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();

Then also same issue, on loading the activity "Hello World" is displayed but timer is not getting displayed, but its showing in the log file. 然后,在加载活动“ Hello World”时也会出现同样的问题,但未显示计时器,而是在日志文件中显示了计时器。

public class CustomView extends View {

 Context context;
 String countValue="";

 public CustomView (Context context) {
    this(context, null);
    this.context=context;
 }

 public CustomView (Context context, AttributeSet attrs) {
    super(context, attrs);
    blackPaint.setColor(getResources().getColor(R.color.colorPrimary));
    this.context=context;
 }



 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
 }

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.WHITE);



    Paint paint = new Paint();
    paint.setColor(Color.BLACK);

    TextPaint textPaint = new TextPaint();
    textPaint.setColor(Color.BLACK);
    textPaint.setTextSize(46);
    textPaint.setTextAlign(Paint.Align.CENTER);
    float textHeight = textPaint.descent() - textPaint.ascent();
    float textOffset = (textHeight / 2) - textPaint.descent();

    RectF bounds = new RectF(0, 0, getWidth(), getHeight());

    canvas.drawText(countValue, bounds.centerX(), bounds.centerY() + textOffset+ 180, textPaint);

}

@Override
public boolean onTouchEvent(MotionEvent event) {

    return true;
}

public void CountDownTimer(final Canvas canvas){

    countDownTimer=new CountDownTimer(31000, 1000) {

        public void onTick(long millisUntilFinished) {
            countValue = millisUntilFinished / 1000+"";
            invalidate();
            Log.d("ShowTimer",millisUntilFinished / 1000+"");
        }

        public void onFinish() {

        }
    }.start();
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM