Android alpha 动画淡入淡出延迟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3298330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android alpha animation fadein fadeout with delays
提问by zegnus
I want to do a very simple alpha animation but I cannot find a valid way.
我想做一个非常简单的 alpha 动画,但我找不到有效的方法。
The idea is to perform this animation over a view:
这个想法是在一个视图上执行这个动画:
- alpha from 0 to 1 of 1 second
- hold alpha at 1 for 5 seconds
- alpha from 1 to 0 of 1 second
- hold alpha at 0 for 5 seconds.
- start again on 1.
- alpha 从 0 到 1 的 1 秒
- 将 alpha 保持在 1 5 秒
- alpha 从 1 到 0 的 1 秒
- 将 alpha 保持在 0 5 秒。
- 1 重新开始。
I've tried to implement that with an AnimationSet as:
我试图用一个 AnimationSet 来实现它:
AnimationSet animationSet = new AnimationSet(true);
Animation animation1 = new AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
animation1.setDuration(1000);
Animation animation2 = new AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
animation2.setDuration(1000);
animation2.setStartOffset(5000);
Animation animation3 = new AlphaAnimation(0.0f, 0.0f);
animation3.setDuration(4000)
animation3.setStartOffset(6000);
animationSet.add(animation1);
animationSet.add(animation2);
animationSet.add(animation3);
etc..
等等..
but it seams that the third animation do a mess with all the alpha animations, I supose that this cause an internal incoherence in the way that Android manage this type of animation.
但看起来第三个动画对所有 alpha 动画都造成了混乱,我认为这会导致 Android 管理此类动画的方式存在内部不连贯性。
Any idea?
任何的想法?
Thank you.
谢谢你。
回答by Sherif elKhatib
Ok Keep in mind these 2 points to solve this
好的 请记住这 2 点来解决这个问题
If I want to animate
1.0f to 0.0f
after 5 seconds with an animation duration of 1 seconds, this is ultimately a 1 second animation with a pause of 5 seconds.To acheive this:
setDuration(1000)
(it has a 1 second duration)setStartOffset(5000)
(it will start after 5 seconds)
如果我想
1.0f to 0.0f
在 5 秒后以 1 秒的动画持续时间进行动画处理,这最终是一个 1 秒的动画,暂停 5 秒。要实现这一点:
setDuration(1000)
(它有 1 秒的持续时间)setStartOffset(5000)
(它会在 5 秒后开始)
You only need 2 animations that will loop forever.
1.
0.0f to 1.0f
with 5 seconds pause and 1 second duration2.
1.0f to 0.0f
with 5 seconds pause and 1 second duration
您只需要 2 个将永远循环的动画。
1.
0.0f to 1.0f
有 5 秒暂停和 1 秒持续时间2.
1.0f to 0.0f
有 5 秒暂停和 1 秒持续时间
And here is the code:
这是代码:
animation1 = new AlphaAnimation(0.0f, 1.0f);
animation1.setDuration(1000);
animation1.setStartOffset(5000);
animation2 = new AlphaAnimation(1.0f, 0.0f);
animation2.setDuration(1000);
animation2.setStartOffset(5000);
textView.startAnimation(animation1);
However to loop forever I will use AnimationListener
because repeatCount is buggy:
但是要永远循环我会使用,AnimationListener
因为 repeatCount 有问题:
animation1 = new AlphaAnimation(0.0f, 1.0f);
animation1.setDuration(1000);
animation1.setStartOffset(5000);
//animation1 AnimationListener
animation1.setAnimationListener(new AnimationListener(){
@Override
public void onAnimationEnd(Animation arg0) {
// start animation2 when animation1 ends (continue)
textView.startAnimation(animation2);
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
animation2 = new AlphaAnimation(1.0f, 0.0f);
animation2.setDuration(1000);
animation2.setStartOffset(5000);
//animation2 AnimationListener
animation2.setAnimationListener(new AnimationListener(){
@Override
public void onAnimationEnd(Animation arg0) {
// start animation1 when animation2 ends (repeat)
textView.startAnimation(animation1);
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
textView.startAnimation(animation1);
回答by amalBit
There is a simpler solution to this.
对此有一个更简单的解决方案。
Lets assume that your view is in state GONE. To animate to its visibility:
让我们假设您的视图处于 GONE 状态。要为其可见性设置动画:
yourView.setVisibility(View.VISIBLE);
yourView.animate().alpha(1).setDuration(300);
Through the same way you can add animation listeners.
通过同样的方式你可以添加动画监听器。
This also works for scale and translation animations.
这也适用于缩放和平移动画。
回答by Akhil
Try This
尝试这个
val shortAnimationDuration = 1000
yourView.apply {
alpha = 0f
animate()
.alpha(1f)
.setDuration(shortAnimationDuration.toLong())
.setListener(object :AnimatorListenerAdapter(){
override fun onAnimationEnd(animation: Animator?) {
//You can add what you want to do after completing animation
}
})
}
This is a Fade inanimation. If you want Fade out you just swap the alpha values.
这是动画的淡入淡出。如果你想要淡出,你只需交换 alpha 值。