Android 使用 ImageView 淡入淡出

标签 android animation imageview fadein fadeout

我在制作幻灯片时遇到了一些问题。

我在 xml 中创建了 2 个用于淡入和淡出的动画:

fadein.xml

    <?xml version="1.0" encoding="UTF-8"?>
       <set xmlns:android="http://schemas.android.com/apk/res/android">
         <alpha android:fromAlpha="0.0" android:toAlpha="1.0" 
          android:interpolator="@android:anim/accelerate_interpolator" 
          android:duration="2000"/>
     </set>

fadeout.xml

    <?xml version="1.0" encoding="UTF-8"?>
       <set xmlns:android="http://schemas.android.com/apk/res/android">
         <alpha android:fromAlpha="1.0" android:toAlpha="0.0" 
          android:interpolator="@android:anim/accelerate_interpolator" 
          android:duration="2000"/>
     </set>

我想做的是使用淡入淡出效果从 ImageView 更改图像,因此当前显示的图像将淡出,而另一张将淡入。 考虑到我已经设置了一个图像,我可以毫无问题地淡出这个图像:

    Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.your_fade_in_anim);
    imageView.startAnimation(fadeoutAnim);

然后,我设置了下一张要显示的图片:

    imageView.setImageBitmap(secondImage);

它只是显示在 imageView 中,当我设置动画时它会隐藏图像,淡入淡出...有什么办法可以解决这个问题,我的意思是,当我这样做 imageView.setImageBitmap( secondImage); 命令,图像不会立即显示,只有在执行淡入动画时才显示?

最佳答案

我想实现与您相同的目标,因此我编写了以下方法,如果您将 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){
                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 使用 ImageView 淡入淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8720626/

相关文章:

java - 无法用图像按钮交换 2 个图像

android - 如何在 Android 中将 subview 添加到 ImageView

Android - Firebase 身份验证不适用于电子邮件/密码设置

r - .swf 由 knitr 和 hook_r2swf 重叠创建

javascript - 旋转 css 3d 对象不能正常工作

animation - Unity - 在运行时改变游戏对象形状的最佳方式

java - 设置图像资源

Android 播放器无法使用 akamai token 身份验证

android - Delphi XE5 AndroidManifest.xml无法设置 minSdkVersion=14 或更大

java - 无法解析方法askCompactPermission