Ahmad Alkurdi
Ahmad Alkurdi

Reputation: 11

Android App: Timer in On Touch

I have a problem with my code. I want to clear canvas (by reset function) when Action_UP after 3 seconds (Here I used a Timer). During this period if event Action_Down OR Action_Move I have to cancel the Timer-Task.

( Action_down & Action_move should Cancel the Timer in Action_up) What I have to do?? please help me.

Thanks

        public boolean onTouch(View view, MotionEvent event) {
            // Check event type

            switch (event.getAction()) {

            // Finger down
            case MotionEvent.ACTION_DOWN:

                    paint.setColor(Color.BLACK);
                    paint.setStrokeWidth(7f);
                    paint.setStrokeJoin(Paint.Join.ROUND);
                    paint.setStrokeCap(Paint.Cap.ROUND);
                    paint.setAntiAlias(true);


                    // Get current position
                    pX = event.getX();
                    pY = event.getY();

                    // Set beginning of path to (posX,posY)

                    path.moveTo(pX, pY);

                    if (start)
                    {
                        mytimer.cancel();
                        start = false;
                    }


                    break;

            // Finger moves
            case MotionEvent.ACTION_MOVE:
                    mX = event.getX();
                    mY = event.getY();

                    // Set position of end of path
                    path.lineTo(mX, mY);


                    // Draw path
                    bitmapCanvas.drawPath(path, paint);

                    // Invalidate canvas (redraw the view)

                    if (start)
                    {
                        mytimer.cancel();
                        start = false;
                    }

                    invalidate();
                    break;

            // Finger up
            case MotionEvent.ACTION_UP:

                    //bitmap = getResizedBitmap(bitmap, 16, 16);
                    //here We have to start our neocogniton :)   

                    mX = event.getX();
                    mY = event.getY();

                    if (mY == pY && mX == pX){                      
                        bitmapCanvas.drawPoint(pX, pY, paint);
                        invalidate();
                    }

                    path.reset();

                    mytimer.schedule(new TimerTask() {                      
                        @Override
                        public void run() {reset();}}, 3000);




                    break;

            }


            return true;
    }

reset()

    public void reset(){
            bitmapCanvas.drawColor(Color.WHITE);
            start = true;    
    }

Upvotes: 1

Views: 980

Answers (1)

MarJan8008
MarJan8008

Reputation: 11

i suppose you got an error on second timer.cancel because this function is canceling timer itself. Try to enter :

 myTimer = new Timer(); 

before

 myTimer.cancel();

Upvotes: 1

Related Questions