Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 1.14 KB  |  hits: 69  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Having a countdown timer on a Canvas using Surfaceview class in Android
  2. public class GameView extends SurfaceView {
  3.  double timeleft =60,000 //60  sec
  4. int clicked=0;
  5.  static double starttime;//time when we started game
  6.  double timeleft=300000;
  7.  
  8.  @Override
  9. protected void onDraw(Canvas canvas) {
  10.    if(clicked==0)
  11.     score.onDraw(canvas,timeleft); // trying to print the score by calling onDraw function in score class
  12.     else {
  13.         double timegone= System.currentTimeMillis() - starttime ;
  14.         timeleft=timeleft-timegone;
  15.         score.onDraw(canvas,timeleft);
  16.     }
  17.  
  18.        public boolean onTouchEvent(MotionEvent event)
  19. {  
  20.     if(clicked==0)
  21.     starttime= System.currentTimeMillis();
  22.  
  23.           clicked++;
  24.     }
  25.    }
  26.  }
  27.        
  28. public void onDraw(Canvas canvas, double time) {
  29.     Paint paint = new Paint();
  30.     int min=(int) (time/60000);
  31.     int sec=(int) ((time)%60000);
  32.     paint.setColor(Color.GREEN);
  33.     paint.setTextSize(20);
  34.     canvas.drawText("Coins:"+Integer.toString(coins),gameView.getWidth()-100, 20, paint);
  35.     canvas.drawText("Time Left:"+Integer.toString(min)+":"+Integer.toString(sec),gameView.getWidth()-140, 40, paint);
  36.  
  37. }