如何从一个图像平滑过渡到另一个图像

时间:2013-07-11 07:12:02

标签: android android-activity android-imageview

我有一个活动,我定期更改 ImageView ,因为我编写了以下代码行。

imageview.setImageUri(resId);

我正在增加资源ID。它工作正常,但是从一个图像突然转换到另一个图像。我不希望这样,我希望图像视图平滑过渡到另一个图像。我怎么能这样做?

3 个答案:

答案 0 :(得分:16)

试试这个

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 :(得分:2)

尝试alpha动画。首先淡出图像视图,在动画结束时,更改资源,然后淡化图像视图。

答案 2 :(得分:1)

要实现平滑过渡,您必须在Android中使用动画,首先阅读以下链接: http://www.vogella.com/articles/AndroidAnimation/article.html

关于动画的stackoverflow有许多类似的问题,网上有很多关于这个主题的教程。在谷歌上进行简单搜索会带来大量的结果

相关问题