2014-02-06 84 views
2

因此,我已implemenented其完美地工作於另一thread幻燈片動畫循環的Android

發現該代碼是下面列出的代碼的片段:

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 

     } 
    }); 
} 

基本上這個代碼接受圖像陣列,並創建一個幻燈片與fadeIn和FadeOut效果。一旦到達最後,動畫就會停止。我如何修改代碼以永久保持動畫效果?

+0

試過for循環直到seq結束? – 2014-02-06 13:55:28

+1

該方法的簽名中有永遠的布爾值,試圖使用它? – ElDuderino

+0

所以你說我必須用for語句替換if語句? – AndroidKrayze

回答

1

找到解決方案夥計們。

只需稍作改動!在其他聲明中,我必須將Forever設置爲false!

  else{ 
      if(forever == false){ 
       animate(imageView, images, 0, forever); 
      } 
     }