用reDraw启动天文台

时间:2013-06-27 03:34:11

标签: android canvas view ondraw chronometer

您好我正在开发一个Android应用程序,它涉及重绘画布和同时启动计时器。有谁知道如何实现这一目标?

当调用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;                         
            }
      }

1 个答案:

答案 0 :(得分:0)

您的invalidate()方法只会调用onDraw()方法,而不是ReDraw的构造函数。 这就是为什么你只重新绘制canvas

在绘制正方形后尝试chrono.start()

即。

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

更新

试试这个

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