Android:如何在动画端运行一些代码

时间:2018-03-27 09:44:22

标签: android android-animation

在Android上我试图创建一个倒计时动画,它做两件事:

  1. 它从绿色变为白色的正方形
  2. 执行时会更改文本视图的上下文
  3. 代码是:

        int colorFrom = getResources().getColor(R.color.green);
        int colorTo = getResources().getColor(R.color.white);
        ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
        colorAnimation.setRepeatCount(ValueAnimator.INFINITE);
        colorAnimation.setRepeatMode(ValueAnimator.RESTART);
        colorAnimation.setDuration(30000); // milliseconds
        colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() 
        {
            @Override
            public void onAnimationUpdate(ValueAnimator animator)
            {
                tv.setBackgroundColor((int) animator.getAnimatedValue());
            }
    
        });
    
        colorAnimation.addListener(new AnimatorListenerAdapter()
        {
            @Override
            public void onAnimationEnd(Animator animation)
            {
                String authcode = getAuthCode(login_session);
                code.setText(authcode);            
            }
        });
    
        colorAnimation.start();
    

    我知道Detecting when ValueAnimator is done,这是我将解决方案移植到我的代码中的地方,但它似乎对我的情况不起作用。绿色到白色淡入淡出可以工作,但是当它完成时它只是重新启动动画而不在文本视图中设置新文本。

    我做错了什么,如何在动画结束时设置新文字?

4 个答案:

答案 0 :(得分:1)

  

试试这个

使用此

colorAnimation.setRepeatCount(0);

而不是这个

colorAnimation.setRepeatCount(ValueAnimator.INFINITE);

示例代码

int colorFrom = getResources().getColor(R.color.green);
        int colorTo = getResources().getColor(R.color.lightWhite);
        ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
        colorAnimation.setRepeatCount(0);
        colorAnimation.setRepeatMode(ValueAnimator.RESTART);
        colorAnimation.setDuration(3000); // milliseconds
        colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animator) {
                tv.setBackgroundColor((int) animator.getAnimatedValue());
            }

        });

        colorAnimation.addListener(new AnimatorListenerAdapter()
        {
            @Override
            public void onAnimationEnd(Animator animation)
            {
                tv.setText("TEST");
            }
        });
        colorAnimation.start();
  

您也可以使用AnimatorListenerAdapter

  

此适配器类提供Animator.AnimatorListene r方法的空实现。任何只关心此侦听器的方法子集的自定义侦听器都可以简单地子类化此适配器类,而不是直接实现接口。

试试这个

public class MainActivity extends AppCompatActivity {

    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        tv = findViewById(R.id.tv);

        int colorFrom = getResources().getColor(R.color.green);
        int colorTo = getResources().getColor(R.color.lightWhite);
        ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
        colorAnimation.setRepeatCount(ValueAnimator.INFINITE);
        //colorAnimation.setRepeatMode(ValueAnimator.RESTART);
        colorAnimation.setDuration(3000); // milliseconds
        colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animator) {
                tv.setBackgroundColor((int) animator.getAnimatedValue());
            }

        });

        colorAnimation.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationCancel(Animator animation) {
                super.onAnimationCancel(animation);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                tv.setText("END");
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                super.onAnimationRepeat(animation);
                tv.setText("REPEAT");
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                tv.setText("START");
            }

            @Override
            public void onAnimationPause(Animator animation) {
                super.onAnimationPause(animation);
                tv.setText("PAUSE");
            }

            @Override
            public void onAnimationResume(Animator animation) {
                super.onAnimationResume(animation);
                tv.setText("RESUME");
            }
        });

        colorAnimation.start();


    }


}

答案 1 :(得分:0)

尝试为callback

添加以下addListener
colorAnimation.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
              String authcode = getAuthCode(login_session);
              code.setText(authcode);
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

答案 2 :(得分:0)

问题在于这行代码

colorAnimation.setRepeatCount(ValueAnimator.INFINITE);

它将保持动画,因此动画结束的回叫永远不会完成。 如果您只是在30秒之后评论该行(正如您在代码中所建议的那样),那么动画结束的回调将会完成。

答案 3 :(得分:0)

设置 RepeatCount = 0

colorAnimation.setRepeatCount(0);

而不是

colorAnimation.setRepeatCount(ValueAnimator.INFINITE);