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

7

我对一个问题感到困惑:

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

btnView.startAnimation(animation);
btnView.setVisibility(View.VISIBLE);

我的代码运行正常,但当我添加以下代码时:

btnView.clearAnimation();

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

4
不清楚你的问题。你想展示你的动画还是清除它? - Raptor
3个回答

6

如果你想在动画完成后清除视图的动画效果,你需要重写视图的Animation Listener并清除动画。

如果你希望视图回到初始位置,只需设置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

            }
        });

2

要在动画完成后清除动画,请将setFillAfter设置为false。


0

使用以下方式:

// start animation
TranslateAnimation anim = new TranslateAnimation( 0, 100 , 0, 100);
anim.setDuration(1000);
anim.setFillAfter( true );
view.startAnimation(anim);

// end animation or cancel that
view.getAnimation().cancel();
view.clearAnimation();

cancel()

取消动画。手动取消动画会调用动画监听器(如果已设置)来通知动画结束。 如果您手动取消了动画,则必须在再次启动动画之前调用reset()。

clearAnimation()

取消此视图的任何动画。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接