Android alpha 动画淡入淡出带延迟
我想做一个非常简单的阿尔法动画,但我找不到有效的方法。
这个想法是在视图上执行此动画:
- 从 0 到 1 的 1 秒的 alpha
- 将 alpha 保持在 1 5 秒,
- 从 1 到 0 的 1 秒
- 将 alpha 保持在 0 5 秒。
- 从 1 重新开始。
我尝试使用 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);
等等。
但是第三个动画似乎弄乱了所有的 alpha 动画,我认为这会导致 Android 管理方式的内部不连贯这种类型的动画。
有什么想法吗?
谢谢。
I want to do a very simple alpha animation but I cannot find a valid way.
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.
I've tried to implement that with an AnimationSet as:
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.
Any idea?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好的,记住这两点来解决这个问题
如果我想在 5 秒后将
1.0f 变为 0.0f
,动画持续时间为 1 秒,这最终是一个 1 秒的动画,暂停 5 秒。要实现这一点:
setDuration(1000)
(持续时间为 1 秒)setStartOffset(5000)
(5秒后开始)您只需要 2 个动画这将永远循环。
1.
0.0f 至 1.0f
,暂停 5 秒,持续时间 1 秒2.
1.0f 到 0.0f
,暂停 5 秒,持续时间 1 秒代码如下:
然而,为了永远循环,我将使用
AnimationListener
,因为repeatCount有问题:Ok Keep in mind these 2 points to solve this
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)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 durationAnd here is the code:
However to loop forever I will use
AnimationListener
because repeatCount is buggy:对此有一个更简单的解决方案。
假设您的视图处于 GONE 状态。要为其可见性设置动画:
通过相同的方式您可以添加动画侦听器。
这也适用于缩放和平移动画。
There is a simpler solution to this.
Lets assume that your view is in state GONE. To animate to its visibility:
Through the same way you can add animation listeners.
This also works for scale and translation animations.
试试这个
这是一个淡入动画。如果您想要淡出,只需交换 alpha 值即可。
Try This
This is a Fade in animation. If you want Fade out you just swap the alpha values.
在我的解决方案中,我在任何通用视图的 util 函数中使用动画,并且我能够通过以这种方式定义内联值来实现这一点:
In my solution, I am using the animation inside a util function for any generic View and I was able to achieved this by defining the values inline this way: