2

So I have implemenented a fragment of code which works perfectly found on another thread

The code is listed below :

    private void animate(final ImageView imageView, final int images[], final int imageIndex, final boolean forever){

    //imageView <-- The View which displays the images
    //images[] <-- Holds R references to the images to display
    //imageIndex <-- index of the first image to show in images[] 
    //forever <-- If equals true then after the last image it starts all over again with the first image resulting in an infinite loop. You have been warned.

    int fadeInDuration = 500; // Configure time values here
    int timeBetween = 3000;
    int fadeOutDuration = 1000;

    imageView.setVisibility(View.INVISIBLE); //Visible of invisible by default
    imageView.setImageResource(images[imageIndex]);

    Animation fadeIn = new AlphaAnimation(0, 1);
    fadeIn.setInterpolator(new DecelerateInterpolator()); //Add this
    fadeIn.setDuration(fadeInDuration);

    Animation fadeOut = new AlphaAnimation(1, 0);
    fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
    fadeOut.setStartOffset(fadeInDuration + timeBetween);
    fadeOut.setDuration(fadeOutDuration);

    AnimationSet animation = new AnimationSet(false); //change to false
    animation.addAnimation(fadeIn);
    animation.addAnimation(fadeOut);
    animation.setRepeatCount(1);
    imageView.setAnimation(animation);

    animation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationEnd(Animation animation) {
            if(images.length -1 > imageIndex){
                animate(imageView, images, imageIndex + 1, forever); // Calls itself until it gets to the end of the array.
            }
            else{
                if(forever == true){
                    animate(imageView, images, 0, forever); //Calls itself to start the animation all over again in a loop if forever = true
                }
            }

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }
    });
}

Basically this code accepts an Array of images and creates a slideshow with fadeIn and FadeOut effects. Once it reaches the end the animation stops. How can I modify the code to keep animating forever?

Community
  • 1
  • 1
AndroidKrayze
  • 349
  • 2
  • 5
  • 21

1 Answers1

1

Found the solution Guys.

Just had to make a slight alteration! On the else statement I had to set Forever to false!

            else{
            if(forever == false){
                animate(imageView, images, 0, forever);
            }
        }
Pang
  • 9,564
  • 146
  • 81
  • 122
AndroidKrayze
  • 349
  • 2
  • 5
  • 21