如何实现图片之间的平滑过渡

11

我有一个活动,需要定期更换ImageView,因此我编写了以下代码。

imageview.setImageUri(resId);

我正在增加资源ID。虽然它运行良好,但一个图像会突然转换为另一个。我不想要那样,我希望图像视图的过渡是平稳的。我该怎么做?

3个回答

17

试试这个

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
        }
    });
}

谢谢您的快速回复。我给您投了赞成票。我正在使用Android 2.3进行开发,而动画功能是在Android 3中添加的。 - Shakeeb Ayaz
不知道我来得是否太晚,但我一直在寻找这样的答案,它真的很棒。无论如何,我发现了另一种重新启动效果的方法。你可以这样做:animate(imageView, images, imageIndex % images.length + 1,forever); 这样索引将始终在范围内。 - Miquel Perez
为什么我将这段代码翻译成 Kotlin 后,AlphaAnimation 就无法工作了呢?翻译后,它告诉我必须为其创建一个函数,尽管它显然是库的一部分。 - GPell7

2

尝试使用alpha动画。首先淡出ImageView,在动画结束时更改资源,然后淡入ImageView。


1
我正在使用Android 2.3进行开发,而动画功能是在Android 3中添加的。 - Shakeeb Ayaz
Alpha动画从API级别1开始提供。http://developer.android.com/reference/android/view/animation/AlphaAnimation.html - Deepak Senapati

1

我正在开发 Android 2.3 版本,在此版本中还未添加动画功能,该功能是在 Android 3 版本中加入的。 - Shakeeb Ayaz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接