1

我正在尝试创建一个倒数计时器并将其显示在画布上。这是我显示它的方式。

public class DrawView extends SurfaceView {
private Paint textPaint = new Paint();
Bitmap GameBg;
DisplayMetrics metrics;
int screenWidth = 0;
int screenHeight = 0;
Rect dest;
Paint paint;
String timer;
public DrawView(Context context) {
     super(context);
     // Create out paint to use for drawing
     textPaint.setARGB(255, 200, 0, 0);
     textPaint.setTextSize(60);
     // This call is necessary, or else the 
     // draw method will not be called. 
     setWillNotDraw(false);

     GameBg = BitmapFactory.decodeResource(getResources(),R.drawable.gembackground);
     metrics = context.getResources().getDisplayMetrics();
     screenWidth = metrics.widthPixels;
     screenHeight = metrics.heightPixels;
     dest = new Rect(0, 0, screenWidth, screenHeight);
     paint = new Paint();
     paint.setFilterBitmap(true);

     new CountDownTimer(60000, 1000) {

         public void onTick(long millisUntilFinished) {
             timer = String.valueOf(millisUntilFinished / 1000);
         }

         public void onFinish() {
         }
         }.start();

     }

     @Override
     protected void onDraw(Canvas canvas){
     // A Simple Text Render to test the display
     canvas.drawBitmap(GameBg, null, dest, paint);
     canvas.drawText(timer, screenWidth - 50, screenHeight - 50, paint);

     }

}

我可以显示计时器,但它不倒计时。有任何想法吗?

4

1 回答 1

1

您永远不会对timer. 尝试这样的事情:

 new CountDownTimer(60000, 1000) {
     public void onTick(long millisUntilFinished) {
         timer = String.valueOf(millisUntilFinished / 1000);
         invalidate(); // Force the View to redraw
     }

     public void onFinish() {}
 }.start();
于 2013-01-12T01:04:47.630 回答