android - 如何从一张图片平滑过渡到另一张图片

标签 android android-activity android-imageview

我有一个定期更改 ImageView 的 Activity ,为此我编写了以下代码行。

imageview.setImageUri(resId);

我正在增加资源 ID。它工作正常,但从一张图片突然过渡到另一张图片。我不想要那个,我想要 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
        }
    });
}

关于android - 如何从一张图片平滑过渡到另一张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17587152/

相关文章:

Android - 我可以使用 MediaRecord/MediaPlayer 而不是 AudioRecord/Track 来执行此操作吗?

android - 如何在 Activity 完成时取消 AsyncTask?

android - 如何在自定义ImageView中设置scaleType和adjustViewBounds

android - 帮助纠正 Android OpenGL 2.0 w/QCAR 中的线缩放

android - 如何更改 ActionMode 中 TextView 的背景颜色

android - 在 Android 模拟器上模拟慢速网络

android - onActivityResult 在 onDestroy 之前被调用

Android - 重新启动 Activity 而无需重新创建它

android-layout - 如何在屏幕上拉伸(stretch)三张图像并保持纵横比?

android - ImageView 不能正确居中缩小图像