Android alpha 动画淡入淡出延迟 [英] Android alpha animation fadein fadeout with delays

查看:53
本文介绍了Android alpha 动画淡入淡出延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个非常简单的 alpha 动画,但我找不到有效的方法.

这个想法是在一个视图上执行这个动画:

  1. alpha 从 0 到 1 的 1 秒
  2. 将 alpha 保持在 1 5 秒
  3. alpha 从 1 到 0 的 1 秒
  4. 将 alpha 保持在 0 5 秒.
  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 秒.>

    要实现这一点:

    1. setDuration(1000)(持续时间为 1 秒)
    2. 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:

  1. alpha from 0 to 1 of 1 second
  2. hold alpha at 1 for 5 seconds
  3. alpha from 1 to 0 of 1 second
  4. hold alpha at 0 for 5 seconds.
  5. 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:

    1. setDuration(1000) (it has a 1 second duration)
    2. 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 duration

    2.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆