0

My problem is trying to figure out how to stop the drawable animation after it iterated though the images (eg. it being back to the grapes image). I certainly can't use the method I implemented in .ACTION.DOWN so can anybody help me with this.

    public boolean onTouchEvent (MotionEvent event){
        Drawable currentFrame, checkFrame;
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (fruitAnimation.isRunning()) {
                fruitAnimation.stop();
                //The variable that will guard the frame number
                int frameNumber = 0;
                //Get the frame of the animation
                //Drawable currentFrame, checkFrame;
                currentFrame = fruitAnimation.getCurrent();
                //checks the position of the frame.
                for (int i = 0; i < fruitAnimation.getNumberOfFrames(); i++) {
                    checkFrame = fruitAnimation.getFrame(i);
                    if (checkFrame == currentFrame) {
                        frameNumber = i;
                        break;
                    }

                }
                String fruit = "";

                switch (frameNumber) {
                    case 0:
                        fruit = "Grapes";
                        break;
                    case 1:
                        fruit = "Lemon";
                        break;
                    case 2:
                        fruit = "Orange";
                        break;
                    case 3:
                        fruit = "Pear";
                        break;
                    case 4:
                        fruit = "Strawberry";
                        break;


                }
                Toast.makeText(this, fruit, Toast.LENGTH_SHORT).show();

            }else {
                fruitAnimation.start();


            }
            return true;
        }

        return super.onTouchEvent(event);
    }

1 Answers1

0

You most likely doing this wrong way. Since there's animation listener you can utilize to monitor your animation, I'd set the animation to one-time run and then in my listener, in its onAnimationEnd() I'd check if I reached my limit and if not, start the animation again.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • I chose your answer since you're the only one that answered the question but what I did was to add `android:oneshot="true"` in the xml file that contained my pictures. –  Apr 24 '15 at 05:44
  • ah, that's correct approach. I for some reason read you want to stop your animation after given X plays. Anyway, glad it works for you – Marcin Orlowski Apr 24 '15 at 07:26