0

嗨,我正在开发一个 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;                         
            }
      }
4

1 回答 1

0

您的invalidate()方法只会调用onDraw()方法而不是ReDraw. 这就是为什么你只canvas重绘。

chrono.start()画完正方形后试试

IE

 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;                         
            }
      }
于 2013-06-27T04:08:37.220 回答