我想有一个2秒动画的ImageView,花1000毫秒淡入,然后1000毫秒淡出。
到目前为止,我在ImageView构造函数中的代码如下:
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setDuration(1000);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);
AnimationSet animation = new AnimationSet(true);
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
this.setAnimation(animation);
当我运行该动画时,什么也没有显示。但是,当我删除其中一个alpha动画时,行为按预期工作。
我已经尝试过的东西:
setFillBefore
、setFillAfter
和setFillEnabled
.LinearInterpolator
to AnimationSet
.的每一种可以想到的组合
发布于 2011-07-26 04:43:54
解决了我自己的问题。解决方案最终基于插值器。
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); //add this
fadeIn.setDuration(1000);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);
AnimationSet animation = new AnimationSet(false); //change to false
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
this.setAnimation(animation);
如果您正在使用Kotlin
val fadeIn = AlphaAnimation(0f, 1f)
fadeIn.interpolator = DecelerateInterpolator() //add this
fadeIn.duration = 1000
val fadeOut = AlphaAnimation(1f, 0f)
fadeOut.interpolator = AccelerateInterpolator() //and this
fadeOut.startOffset = 1000
fadeOut.duration = 1000
val animation = AnimationSet(false) //change to false
animation.addAnimation(fadeIn)
animation.addAnimation(fadeOut)
this.setAnimation(animation)
发布于 2012-08-31 03:08:00
我知道这问题已获解答,但……
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="1000"
android:repeatCount="infinite"
android:repeatMode="reverse"
/>
快速和简单的方法来快速做一个淡入和淡出与自我重复。尽情享受
编辑:在您的活动中添加以下内容:
yourView.startAnimation(AnimationUtils.loadAnimation(context, R.anim.yourAnimation));
发布于 2015-10-08 02:06:37
viewToAnimate.animate().alpha(1).setDuration(1000).setInterpolator(new DecelerateInterpolator()).withEndAction(new Runnable() {
@Override
public void run() {
viewToAnimate.animate().alpha(0).setDuration(1000).setInterpolator(new AccelerateInterpolator()).start();
}
}).start();
https://stackoverflow.com/questions/6796139
复制相似问题