Android的奥飞动漫淡入淡出与延迟 [英] Android alpha animation fadein fadeout with delays

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

问题描述

我希望做一个很简单的奥飞动漫,但我不能找到有效的方法。

我们的想法是在一个视图来执行此动画:

  1. 阿尔法从0到1 1秒
  2. 在保持阿尔法在1 5秒
  3. 阿尔法从1到0 1秒
  4. 在保持阿尔法在0 5秒。
  5. 在再次1日开始。

我一直在努力,实现了与AnimationSet为:

  AnimationSet animationSet =新AnimationSet(真正的);

动画动画1 =新AnimationUtils.loadAnimation(这一点,android.R.anim.fade_in);
animation1.setDuration(1000);

动画animation2 =新AnimationUtils.loadAnimation(这一点,android.R.anim.fade_out);
animation2.setDuration(1000);
animation2.setStartOffset(5000);

动画动画3 =新AlphaAnimation(0.0,0.0);
animation3.setDuration(4000)
animation3.setStartOffset(6000);

animationSet.add(动画1);
animationSet.add(animation2);
animationSet.add(动画3);
 

等。

但它接缝,第三动画做的乱七八糟的所有的α动画,我supose,这会导致该机器人管理这种类型的动画方式内部不一致。

你知道吗?

感谢你。

解决方案

确定请记住这2点来解决这个


  • 如果我想动画 1.0F为0.0f 5秒1秒的动画时间后,这是最后1秒的动画暂停的5秒

    要达致这:

    1. setDuration(1000)(它有1秒的持续时间)
    2. setStartOffset(5000)(将在5秒后开始)

  • 您只需要2个动画,将永远循环下去。

    1。 0.0至1.0F ,拥有5秒停顿1秒持续时间

    2 1.0F为0.0f ,拥有5秒停顿1秒持续时间


这里是code:

 动画1 =新AlphaAnimation(0.0,1.0F);
    animation1.setDuration(1000);
    animation1.setStartOffset(5000);

    animation2 =新AlphaAnimation(1.0F,0.0);
    animation2.setDuration(1000);
    animation2.setStartOffset(5000);

    textView.startAnimation(动画1);
 

然而,为了永远循环下去我会用 AnimationListener 因为的repeatCount有错误:

 动画1 =新AlphaAnimation(0.0,1.0F);
    animation1.setDuration(1000);
    animation1.setStartOffset(5000);

    //动画1 AnimationListener
    animation1.setAnimationListener(新AnimationListener(){

        @覆盖
        公共无效onAnimationEnd(动画为arg0){
            //启动animation2当动画1结束(继续)
            textView.startAnimation(animation2);
        }

        @覆盖
        公共无效onAnimationRepeat(动画为arg0){
            // TODO自动生成方法存根

        }

        @覆盖
        公共无效onAnimationStart(动画为arg0){
            // TODO自动生成方法存根

        }

    });

    animation2 =新AlphaAnimation(1.0F,0.0);
    animation2.setDuration(1000);
    animation2.setStartOffset(5000);

    // animation2 AnimationListener
    animation2.setAnimationListener(新AnimationListener(){

        @覆盖
        公共无效onAnimationEnd(动画为arg0){
            //启动动画1时animation2结束(重复)
            textView.startAnimation(动画1);
        }

        @覆盖
        公共无效onAnimationRepeat(动画为arg0){
            // TODO自动生成方法存根

        }

        @覆盖
        公共无效onAnimationStart(动画为arg0){
            // TODO自动生成方法存根

        }

    });

    textView.startAnimation(动画1);
 

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的奥飞动漫淡入淡出与延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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