2013-06-27 71 views
0

您好我正在研究一個Android應用程序,其中涉及重繪畫布和同時啓動天文臺。任何人都知道這是如何實現的?從reDraw開始計時

我試圖在調用invalidate()時在View類中調用chronometer.start。但是,只有畫布被重新繪製,天文臺根本沒有啓動。

編輯: 這是我試過的代碼:

public class ReDraw extends View{ 
    public ReDraw(Context context){ 
      super(context); 
      this.selfPointer = this; 
      setFocusable(true); 
      chrono(context); 
      } 


      public void chrono(Context context){ 
       chrono = new Chronometer(context); 

       chrono.setOnChronometerTickListener(new OnChronometerTickListener(){ 
        public void onChronometerTick(Chronometer arg){ 

         elapsedTime = (SystemClock.elapsedRealtime() - arg.getBase())/1000; 
         long milliseconds= (long) (elapsedTime/60); 

         String millisec=Long.toString(milliseconds); 
         arg.setText(millisec); 
        } 
       }); 
       chrono.setBase(SystemClock.elapsedRealtime()); 
        chrono.start(); 
       } 


     } 

    protected void onDraw(Canvas Square) 
      { 
      super.onDraw(Square); 
      Paint squareColor = new Paint(); 
       squareColor.setColor(Color.BLACK); 
       Square.drawRect(200,100,200,100, squareColor); 
       return; 
       } 

      } 

    public boolean onTouchEvent(MotionEvent event) 
      { 

       if (event.getAction() == MotionEvent.ACTION_DOWN) 
       { 
        invalidate(); 
       } 
       return;       
      } 
     } 
+0

請發表您的代碼,以便我們可以很容易弄清楚? –

+0

發佈了代碼! @MichaelShrestha – shannon

回答

0

invalidate()方法只會調用onDraw()方法不是ReDraw的構造。 這就是爲什麼你只有canvas重繪。

嘗試chrono.start()繪製正方形後

Square.drawRect(200,100,200,100, squareColor); 
chrono.start() 

UPDATE

試試這個

public class ReDraw extends View{ 
    String currentTime="00:00:00"; 
    public ReDraw(Context context){ 
      super(context); 
      this.selfPointer = this; 
      setFocusable(true); 
      chrono(context); 
      } 


      public void chrono(Context context){ 
       chrono = new Chronometer(context); 

       chrono.setOnChronometerTickListener(new OnChronometerTickListener(){ 
        public void onChronometerTick(Chronometer arg){ 
         String HH =((elapsedTime/3600) < 10 ? "0" : "") + (elapsedTime/3600); 
         String MM =((elapsedTime/60) < 10 ? "0" : "") + (elapsedTime/60); 
         String SS =((elapsedTime % 60) < 10 ? "0" : "") + (elapsedTime % 60); 
         currentTime = HH+":"+MM+":"+SS; 
         elapsedTime = (SystemClock.elapsedRealtime() - arg.getBase())/1000; 
         arg.setText(currentTime); 
        } 
       }); 
       chrono.setBase(SystemClock.elapsedRealtime()); 
        chrono.start(); 
       } 


     } 

    protected void onDraw(Canvas Square) 
      { 
      super.onDraw(Square); 
      Paint squareColor = new Paint(); 
       squareColor.setColor(Color.BLACK); 
       Square.drawRect(200,100,200,100, squareColor); 
       return; 
       } 

      } 

    public boolean onTouchEvent(MotionEvent event) 
    { 

       if (event.getAction() == MotionEvent.ACTION_DOWN) 
       { 
        chrono.start(); 
       } 
       return true;       
      } 
     } 
+0

有什麼辦法可以調用ReDraw的構造函數嗎?或者將計時器與提款一起撥打? – shannon

+0

你有沒有試過更新的答案? –

+0

哦,對不起,我只看到你的更新ans後評論..我現在試試! – shannon