如何以正确的方式清除动画

daw*_*wei 6 animation android

我对一个问题感到困惑:

Animation animation = new TranslateAnimation(0, 0, 200, 0);
animation.setDuration(800);
animation.setFillAfter(true);

btnView.startAnimation(animation);
btnView.setVisibility(View.VISIBLE);
Run Code Online (Sandbox Code Playgroud)

我正确地运行此代码,但是当我添加以下代码时:

btnView.clearAnimation();
Run Code Online (Sandbox Code Playgroud)

动画无法显示,此后如何清除动画?

SHA*_*NDA 6

如果您想在动画完成后清除视图的动画

您必须覆盖视图的动画侦听器,然后清除动画

如果你想让你的视图回到初始位置,只需setFillafter(false);

animation.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {


            }

            @Override
            public void onAnimationRepeat(Animation animation) {


            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO set params of the view to required position

            }
        });
Run Code Online (Sandbox Code Playgroud)