Android alpha 动画淡入淡出延迟 [英] Android alpha animation fadein fadeout with delays
问题描述
我想做一个非常简单的 alpha 动画,但我找不到有效的方法.
这个想法是在一个视图上执行这个动画:
- alpha 从 0 到 1 的 1 秒
- 将 alpha 保持在 1 5 秒
- alpha 从 1 到 0 的 1 秒
- 将 alpha 保持在 0 5 秒.
- 从 1 重新开始.
我尝试使用 AnimationSet 来实现它:
AnimationSet animationSet = new AnimationSet(true);动画 animation1 = new AnimationUtils.loadAnimation(this, android.R.anim.fade_in);animation1.setDuration(1000);动画 animation2 = new AnimationUtils.loadAnimation(this, android.R.anim.fade_out);animation2.setDuration(1000);animation2.setStartOffset(5000);动画 animation3 = new AlphaAnimation(0.0f, 0.0f);animation3.setDuration(4000)animation3.setStartOffset(6000);动画集.添加(动画1);animationSet.add(animation2);animationSet.add(animation3);
等等.
但看起来第三个动画与所有的 alpha 动画都搞砸了,我认为这会导致 Android 管理此类动画的方式存在内部不连贯性.
有什么想法吗?
谢谢.
Ok记住这2点来解决这个问题
<小时>如果我想在 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 秒
这是代码:
animation1 = new AlphaAnimation(0.0f, 1.0f);animation1.setDuration(1000);animation1.setStartOffset(5000);动画 2 = 新 AlphaAnimation(1.0f, 0.0f);animation2.setDuration(1000);animation2.setStartOffset(5000);textView.startAnimation(animation1);
但是为了永远循环,我将使用 AnimationListener
因为 repeatCount 有问题:
animation1 = new AlphaAnimation(0.0f, 1.0f);animation1.setDuration(1000);animation1.setStartOffset(5000);//animation1 AnimationListeneranimation1.setAnimationListener(new AnimationListener(){@覆盖公共无效onAnimationEnd(动画arg0){//当动画 1 结束时启动动画 2(继续)textView.startAnimation(animation2);}@覆盖公共无效onAnimationRepeat(动画arg0){//TODO 自动生成的方法存根}@覆盖公共无效onAnimationStart(动画arg0){//TODO 自动生成的方法存根}});动画 2 = 新 AlphaAnimation(1.0f, 0.0f);animation2.setDuration(1000);animation2.setStartOffset(5000);//animation2 AnimationListeneranimation2.setAnimationListener(new AnimationListener(){@覆盖公共无效onAnimationEnd(动画arg0){//当动画 2 结束时启动动画 1(重复)textView.startAnimation(animation1);}@覆盖公共无效onAnimationRepeat(动画arg0){//TODO 自动生成的方法存根}@覆盖公共无效onAnimationStart(动画arg0){//TODO 自动生成的方法存根}});textView.startAnimation(animation1);
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.
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 duration
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:
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);
这篇关于Android alpha 动画淡入淡出延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!