如何循环淡入和淡出多个图像的动画?

时间:2017-05-21 19:11:50

标签: java android android-studio android-animation

请帮我解决这个问题。如何为多个图片创建fade-infade-out动画?我如何loop淡入淡出多个图像的动画?

{
    final ImageView image = (ImageView)findViewById(R.id.imageView2);
    final Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    final Animation animationFadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out);

    Animation.AnimationListener animListener = new Animation.AnimationListener(){

        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            image.setImageResource(R.drawable.picture001);
            image.startAnimation(animationFadeIn);
        }
    };
    image.startAnimation(animationFadeOut);
    animationFadeOut.setAnimationListener(animListener);
}

1 个答案:

答案 0 :(得分:1)

添加count变量并在onAnimationEnd()中使用此变量,以便在上一张图片animation结束时更改图片。

以下是工作代码:

{
    final ImageView image = (ImageView)findViewById(R.id.imageView2);
    final Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    final Animation animationFadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out);

    Animation.AnimationListener animListener = new Animation.AnimationListener(){

        // Required to change the image
        int count = 0;

        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {

            if (animation == animationFadeIn) {

                // Start fade-out animation
                image.startAnimation(animationFadeOut);

            } else if (animation == animationFadeOut) {

                count++;

                // Set next image after fading out previous image
                switch (count) {
                    case 1:
                        image.setImageResource(R.drawable.picture002);
                        image.startAnimation(animationFadeIn);
                        break;
                    case 2:
                        image.setImageResource(R.drawable.picture003);
                        image.startAnimation(animationFadeIn);
                        break;
                    case 3:
                        image.setImageResource(R.drawable.picture004);
                        image.startAnimation(animationFadeIn);
                        break;
                    default:
                        break;
                }
            }
        }
    };

    // Set listener to animation
    animationFadeIn.setAnimationListener(animListener);
    animationFadeOut.setAnimationListener(animListener);

    // Start fade-in animation
    image.setImageResource(R.drawable.picture001);
    image.startAnimation(animationFadeIn);

}

<强> activity.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

<强> fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />

</set>

<强> fade_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="0.0" />

</set>

希望这会有所帮助〜