android - 如何在设置新图像资源时设置 Android 动画

标签 android android-imageview android-animation

我的布局中有一个 ImageView。单击按钮后。我在做这个

Image=(ImageView)rootView.findViewById("Imagetag");
Image.setImageResource(R.drawable.image2);

当我从 Image1 切换到 Image2 时。我想要执行动画。任何事情都可以..Cardflip.FadeIn/Fadeout。

最佳答案

你必须导入:

import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;

private Animation animFade;

animFade = AnimationUtils.loadAnimation(this, R.anim.fadeout);

您可以为按钮设置 onClickListener 并添加以下代码:

        animFade.setAnimationListener(new AnimationListener() {
            public void onAnimationStart(Animation animation) {}
            public void onAnimationRepeat(Animation animation) {}
            public void onAnimationEnd(Animation animation) {
                       // when fadeout animation ends, fade in your second image
            }
        });
        yourfirstimage.startAnimation(animFade);

res/anim 中的动画创建一个 XML 文件(例如 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="3000" android:repeatCount="0"/>
</set>

希望这段代码能让您了解动画的工作原理。如果您需要更多说明,请告诉我。

注意:始终在 UI 线程之外执行动画。

快乐编码。

关于android - 如何在设置新图像资源时设置 Android 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18870871/

相关文章:

android - 如何为 Android 单选按钮使用 setTextColor?

java - 如何从自定义对话框中获取数据并在 recyclerview 中创建一个新项目?

java - 在Android中使用ImageView控件创建错误

java - OnClick 更改图像的 2D 数组

Android Layout Animations from bottom to top and top to bottom on ImageView click

Android ObjectAnimator 动画 translationY 在所有屏幕尺寸上的比例相同

android - 动画 : fillEnable, fillBefore 文档矛盾;语义包括 fillAfter

java - 如何解决不抽象和不重写抽象方法onTabReselected

android - 图像在布局中加载时间太长?

android - ItemTouchHelper 和 SwipeRefreshLayout (RecyclerView)