Android动画的连续循环没有发生

时间:2013-06-07 02:56:35

标签: arrays loops android-animation fadein fadeout

我刚从此链接Android fade in and fade out with ImageView复制了以下代码 但我不明白为什么连续循环没有发生。我想也许(永远)变量没有要测试的条件的初始值。有人会帮我弄清楚我该怎样做才能使这段代码完美运作?...

(我复制并粘贴此代码以提出问题的原因是我没有权利对原始帖子发表评论,对不起。)

我希望有人可以帮助我......谢谢

ImageView demoImage = (ImageView) findViewById(R.id.DemoImage);
 int imagesToShow[] = { R.drawable.image1, R.drawable.image2,R.drawable.image3 };

animate(demoImage, imagesToShow, 0,false);  



 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 or invisible by default - this will apply when the animation ends
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() {
    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
            }
        }
    }
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub
    }
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub
    }
});

}

1 个答案:

答案 0 :(得分:0)

forever的值为true时,在此代码中返回无限循环。但在代码的开头,函数中有false

尝试

替换

animate(demoImage, imagesToShow, 0,false);// at line 3

通过

animate(demoImage, imagesToShow, 0,true);