java - 幻灯片动画循环 Android

标签 java android android-animation android-resources

所以我实现了一段代码,它在另一个 thread 上完美运行

代码如下:

    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

        }
    });
}

基本上,此代码接受图像数组并创建具有淡入淡出效果的幻灯片。一旦到达终点,动画就会停止。如何修改代码以永远保持动画效果?

最佳答案

伙计们找到了解决方案。

只需要稍微改动一下!在 else 语句中,我必须将 Forever 设置为 false!

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

关于java - 幻灯片动画循环 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21604504/

相关文章:

android - 您应用的 AndroidManifest.xml 中的元数据标记没有正确的值

android - 在android studio中有没有办法在不删除模块的情况下禁用它?

android - 在 ImageView 中翻转和更改图像

android - Google Maps v2 上的动画透明圆圈动画不正确

java - 如何在 @PostConstruct 中模拟 bean?

java - 没有找到合适的 add(TextField) 方法?

java - Spring 中的内容类型和@ResponseBody

android - 未在路径 : DexPathList 上找到类 "android.support.v7.internal.widget.TintManager"

java - Java 中的 Amazon S3 策略签名

android - 如果在窗外查看,如何强制启动动画