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

Untitled

By: a guest on Jul 24th, 2012  |  syntax: None  |  size: 1.39 KB  |  hits: 13  |  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. Create a counter in game android
  2. public class GameView extends View{
  3.  
  4. private int count = 200;
  5. private TimeCountThread timeCountThread;
  6.  
  7. public GameView(Context context, AttributeSet attrs) {
  8.     super(context, attrs);
  9.     timeCountThread = new TimeCountThread();
  10.             timeCountThread.start();
  11. }
  12. protected void onDraw(Canvas canvas) {
  13.     Paint paint = new Paint();
  14.     paint.setColor(Color.WHITE);
  15.     canvas.drawText("Time :"+count, 10, 35, paint);
  16. }
  17.  
  18. public class TimeCountThread extends Thread{
  19.     public void run(){
  20.         while(count > 0){
  21.             try {
  22.                 sleep(1000);
  23.                 count--;
  24.             } catch (InterruptedException e) {
  25.                 e.printStackTrace();
  26.             }
  27.         }
  28.     }
  29. }
  30.  
  31. public boolean onTouchEvent(MotionEvent event){
  32.     if(event.getAction()==MotionEvent.ACTION_DOWN){
  33.         int x1=(int) event.getX();
  34.         int y1=(int) event.getY();
  35.         Toast.makeText(getContext(), "x1 = "+x1+", y1 ="+y1,1).show();
  36.     }
  37.     return true;
  38. }
  39. }
  40.        
  41. I want creat a counter .It begin from 200 and then 1s reduced about 0
  42.        
  43. Handler handler=new Handler();
  44. Runnable r=new Runnable() {
  45.     public void run() {
  46.         count--;
  47.         if(count > 0) {
  48.             handler.postDelayed(this, 1000);
  49.         }              
  50.     }
  51. };
  52. if(count > 0) {
  53.     handler.postDelayed(r, 1000);
  54. }
  55.        
  56. timeCountThread = new TimeCountThread();
  57.         timeCountThread.start();