Android的淡入和淡出与ImageView的Android、ImageView

2023-09-12 02:43:48 作者:Gloaming 薄暮城

我有一个幻灯片我要建一些麻烦。

I'm having some troubles with a slideshow I'm building.

我创建2动画在XML的淡入和淡出:

I've created 2 animations in xml for fade in and fade out:

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>

什么Im'trying做的,是从使用淡入淡出效果的ImageView的改变图像,因此当前显示的图像会淡出,而另外一个会褪色。 考虑到我有一个形象已经设置,我可以淡出这个图片没有问题,这一点:

What Im'trying to do, is to change images from an ImageView using the fade effect, so the currently displayed image will fade out, and another one will fade in. Considering that I have an image already set, I can fadeout this Image without problem, with this:

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

不过,我设置要显示的下一个图像:

But then, I set the next image to be displayed:

    imageView.setImageBitmap(secondImage);

这只是显示在ImageView的,当我设置动画来隐藏图像,淡入它......有什么办法来解决这个问题,我的意思是,当我的 imageView.setImageBitmap( secondImage); 的命令,图像不会立即显示了起来,并且只执行动画淡入时

It just shows up in the imageView, and when i set the animation it hides the image, the fade it in... Is there any way to fix that, I mean, when I do imageView.setImageBitmap(secondImage); command, the image do not shows up immediately, and only when the fade in animation is executed?

推荐答案

要实现这一点,你已经开始了,你就需要添加一个AnimationListener这样就可以检测到开始和动画结束。当onAnimationEnd()的淡出被调用时,你可以设置你的ImageView对象View.INVISIBLE的知名度,切换图像,并开始你的淡入动画 - 你会需要另一个AnimationListener这里。当您收到onAnimationEnd()在动画的渐变,设置ImageView的是View.VISIBLE,这应该给你你要寻找的效果。

To implement this the way you have started, you'll need to add an AnimationListener so that you can detect the beginning and ending of an animation. When onAnimationEnd() for the fade out is called, you can set the visibility of your ImageView object to View.INVISIBLE, switch the images and start your fade in animation - you'll need another AnimationListener here too. When you receive onAnimationEnd() for your fade in animation, set the ImageView to be View.VISIBLE and that should give you the effect you're looking for.

我以前实现类似的效果,但我用一个 ViewSwitcher 有2 ImageViews而不是单一ImageView的。您可以为ViewSwitcher的in和走出去的动画设置,在您的淡入和淡出,因此它可以管理AnimationListener实施。然后,所有你需要做的是备用的2 ImageViews之间。

I've implemented a similar effect before, but I used a ViewSwitcher with 2 ImageViews rather than a single ImageView. You can set the "in" and "out" animations for the ViewSwitcher with your fade in and fade out so it can manage the AnimationListener implementation. Then all you need to do is alternate between the 2 ImageViews.

编辑: 要多一点有用的,这里是如何使用ViewSwitcher一个简单的例子。我已经包括在 https://github.com/aldryd/imageswitcher 的完整源代码。

To be a bit more useful, here is a quick example of how to use the ViewSwitcher. I have included the full source at https://github.com/aldryd/imageswitcher.

activity_main.xml

    <ViewSwitcher
        android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:inAnimation="@anim/fade_in"
        android:outAnimation="@anim/fade_out" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitCenter"
            android:src="@drawable/sunset" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitCenter"
            android:src="@drawable/clouds" />
    </ViewSwitcher>

MainActivity.java

    // Let the ViewSwitcher do the animation listening for you
    ((ViewSwitcher) findViewById(R.id.switcher)).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ViewSwitcher switcher = (ViewSwitcher) v;

            if (switcher.getDisplayedChild() == 0) {
                switcher.showNext();
            } else {
                switcher.showPrevious();
            }
        }
    });