0

Hi I am working on an android application which involves redrawing of the canvas and starting of chronometer at the same time. Anybody knows how can this be achieved?

I have tried to call chronometer.start in View class when invalidate() is called. However, only the canvas is redrawn and the chronometer did not start at all.

EDIT: Here's the code I tried:

 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;                         
            }
      }
shannon
  • 579
  • 2
  • 10
  • 22

1 Answers1

0

Your invalidate() method will only call onDraw() method not the constructor of the ReDraw. That's why u only the canvas is redrawn.

try chrono.start() after drawing the square

i.e.

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

UPDATE

try this

 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;                         
            }
      }
Michael Shrestha
  • 2,547
  • 19
  • 30