动画淡入淡出

31

使用这段代码,我只有一个渐入效果,我正在寻找如何添加一个渐出效果。我已经添加了另一个名为“fadeout”的xml文件,但我无法将它集成到我的代码中。

ImageView imageView = (ImageView)findViewById(R.id.imageView);
Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fadein);
imageView.startAnimation(fadeInAnimation);

button1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        imageView.startAnimation(fadeInAnimation);
    }
}

淡入效果.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="Your Duration(in milisecond)"
    android:repeatCount="infinite"/>
 </set>
9个回答

40

这是我的解决方案。它使用AnimatorSet。AnimationSet库太有bug了,无法正常工作。这提供了淡入和淡出之间流畅无缝的无限过渡。

public static void setAlphaAnimation(View v) {
    ObjectAnimator fadeOut = ObjectAnimator.ofFloat(v, "alpha",  1f, .3f);
    fadeOut.setDuration(2000);
    ObjectAnimator fadeIn = ObjectAnimator.ofFloat(v, "alpha", .3f, 1f);
    fadeIn.setDuration(2000);

    final AnimatorSet mAnimationSet = new AnimatorSet();

    mAnimationSet.play(fadeIn).after(fadeOut);

    mAnimationSet.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            mAnimationSet.start();
        }
    });
    mAnimationSet.start();
}

如何停止这个动画? - Maveňツ

20

淡出图像后,图像会以无淡出效果的方式显示,淡入同理。 - Pol Hallen

18

我一直在使用 Kotlin(向每个人推荐),所以语法可能有些不同。 我所做的就是简单地调用:

v.animate().alpha(0f).duration = 200

我认为在Java中应该是以下这样:

v.animate().alpha(0f).setDuration(200);

尝试:

private void hide(View v, int duration) {
    v.animate().alpha(0f).setDuration(duration);
}

private void show(View v, int duration) {
    v.animate().alpha(1f).setDuration(duration);
}

1
你忘记在最后加上“;”了,像这样 = v.animate().alpha(0f).setDuration(200); - Ashvin Bhagat

2
根据文档 AnimationSet
代表应该一起播放的动画组。每个单独动画的变换被组合成单一的变换。如果AnimationSet设置了任何其子元素也设置的属性(例如,duration或fillBefore),则AnimationSet的值将覆盖子元素的值。
AnimationSet mAnimationSet = new AnimationSet(false); //false means don't share interpolators

如果这个动画集中的所有动画都应该使用与此AnimationSet相关联的插值器,请传递true。如果每个动画都应该使用自己的插值器,则传递false。
ImageView imageView= (ImageView)findViewById(R.id.imageView);
Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_in);
Animation fadeOutAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_out);
mAnimationSet.addAnimation(fadeInAnimation);
mAnimationSet.addAnimation(fadeOutAnimation);
imageView.startAnimation(mAnimationSet);

我希望这能对你有所帮助。


1
我们可以简单地使用:
public void animStart(View view) {
    if(count==0){
        Log.d("count", String.valueOf(count));
        i1.animate().alpha(0f).setDuration(2000);
        i2.animate().alpha(1f).setDuration(2000);
        count =1;
    }
    else if(count==1){
        Log.d("count", String.valueOf(count));
        count =0;
        i2.animate().alpha(0f).setDuration(2000);
        i1.animate().alpha(1f).setDuration(2000);
    }
}

i1和i2在onCreateView()中被定义为:

    i1 = (ImageView)findViewById(R.id.firstImage);
    i2 = (ImageView)findViewById(R.id.secondImage);

count是一个类变量,初始值为0。

XML文件如下:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
        <ImageView
            android:id="@+id/secondImage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="animStart"
            android:src="@drawable/second" />
      <ImageView
          android:id="@+id/firstImage"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"

          android:onClick="animStart"
          android:src="@drawable/first" />
    </RelativeLayout>
@drawable/first@drawable/second 是位于 res 文件夹中的 drawable 文件夹中的图片。

1
你也可以用 Kotlin 这样做:

    contentView.apply {
        // Set the content view to 0% opacity but visible, so that it is visible
        // (but fully transparent) during the animation.
        alpha = 0f
        visibility = View.VISIBLE

        // Animate the content view to 100% opacity, and clear any animation
        // listener set on the view.
        animate()
                .alpha(1f)
                .setDuration(resources.getInteger(android.R.integer.config_mediumAnimTime).toLong())
                .setListener(null)
    }

阅读更多关于此的Android文档


1
今天我卡在这个问题上了。这个方法对我有用:

    @JvmStatic
    fun setFadeInFadeOutAnimation(v: View?) {
        val fadeIn = ObjectAnimator.ofFloat(v, "alpha", 0.0f, 1f)
        fadeIn.duration = 300
        val fadeOut = ObjectAnimator.ofFloat(v, "alpha", 1f, 0.0f)
        fadeOut.duration = 2000

        fadeIn.addListener(object : AnimatorListenerAdapter(){
            override fun onAnimationEnd(animation: Animator?) {
                fadeOut.start()
            }
        })
        fadeIn.start()
    }

0
请尝试以下代码实现重复淡出/淡入动画。
AlphaAnimation anim = new AlphaAnimation(1.0f, 0.3f);
    anim.setRepeatCount(Animation.INFINITE);
    anim.setRepeatMode(Animation.REVERSE);
    anim.setDuration(300);
view.setAnimation(anim); // to start animation
view.setAnimation(null); // to stop animation

0

对于淡入淡出效果,首先需要在你的动画对象上添加这行代码。

.animate().alpha(1).setDuration(2000);

例如我使用了LottieAnimation


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接