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

标签 java android android-studio android-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(){

        @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);
}

最佳答案

添加一个 count 变量,并在 onAnimationEnd() 中使用此变量,以便在上一个图像动画结束时更改图像。

这是工作代码:

{
    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>

希望对你有帮助~

关于java - 如何循环播放多个图像的淡入和淡出动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44100796/

相关文章:

android-studio - Android TV模拟器:方向错误

java -> 无法隔离参数 com.android.build.gradle

java - 使用java在幻灯片中创建段落项目符号

android - 以编程方式清除 Android 应用程序中的缓存

android - Android SDK 中的 SQLite 源代码

android - transformClassesWithDesugarForDebug 出错

java - 如何将自定义菜单栏项放置在 map 菜单中的所需位置

Java 日志记录 : Log4j Version2. x:显示终端客户端调用方的方法(不是中间日志记录帮助程序方法)

android - 依赖于 Google-App-Engine 模块的 Aar 文件

android - flutter中如何定义变量和常量?