如何在Android中在动画结束后删除一个动画视图?

18

我正在使用一个RelativeLayout的从右向左移动的动画。

我尝试在onAnimationEnd() 中将可见性设置为 'GONE',但是它不起作用。 动画视图仍然停留在它停止的位置。

这是我使用的代码:

创建从右到左的动画:

TranslateAnimation animate = new TranslateAnimation(0,-rlImages.getWidth()/2,0,0);
animate.setDuration(1000);
animate.setFillAfter(true); 

将动画设置为布局:

centre_leftanimate.startAnimation(animate);

给动画添加监听器:

animate.setAnimationListener(new AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        // TODO Auto-generated method stub
        centre_leftanimate.setVisibility(View.GONE);   // I wants to make the visibility of view to gone,but this is not working                                                         
        half_left.setVisibility(View.VISIBLE);
    }
});
如何使动画视图在动画结束后变为不可见?
请提供建议。
6个回答

25

我曾经遇到过完全相同的问题,只不过我实际上删除了正在动画中的视图,但是动画仍然存在!

简单的解决方法是取消动画,然后你可以隐藏或删除视图。

animatedView.getAnimation().cancel();

或者如果您仍然拥有动画对象:

animation.cancel();

编辑:似乎上述解决方案中还有一些遗留问题,因此我添加了这个:

animatedView.setAnimation(null);

11
我们为什么不直接调用animatedView.clearAnimation()呢? - HendraWD

2

I had the same problem and in my case removing

android:fillAfter="true"

在动画中负责移除视图的代码解决了我的问题。但是其他人可能有更好的代码。
viewToHide.startAnimation(animationForHide);
viewToHide.setVisibility(View.GONE);

2
我知道这是一个老问题,但今天我遇到了这个问题,并且我想展示一下我是如何解决它的,因为尽管已经发布的答案有所帮助,但它们中没有一个完全适用于我的情况。
在我的情况下,我动态创建了一个自定义视图,应用了2个动画(透明度和平移),并在动画完成后删除了该视图。这是我是如何做的:
//Create fade out animation
AlphaAnimation fadeOut = new AlphaAnimation(1f, 0f);
fadeOut.setDuration(1000);
fadeOut.setFillAfter(true);
//Create move up animation
TranslateAnimation moveUp = new TranslateAnimation(0, 0, 0, -getHeight() / 6);
moveUp.setDuration(1000);
moveUp.setFillAfter(true);
//Merge both animations into an animation set
AnimationSet animations = new AnimationSet(false);
animations.addAnimation(fadeOut);
animations.addAnimation(moveUp);
animations.setDuration(1000);
animations.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) { }
        @Override
        public void onAnimationEnd(Animation animation) {
            //Hide the view after the animation is done to prevent it to show before it is removed from the parent view
            view.setVisibility(View.GONE);
            //Create handler on the current thread (UI thread)
            Handler h = new Handler();
            //Run a runnable after 100ms (after that time it is safe to remove the view)
            h.postDelayed(new Runnable() {
                @Override
                public void run() {
                    removeView(view);
                }
            }, 100);
        }
        @Override
        public void onAnimationRepeat(Animation animation) { }
    });
view.startAnimation(animations);

请注意,这是在自定义视图(扩展了FrameLayout)内完成的,所有内容都在UI线程上运行。

1
您可以在需要时设置视图的可见性。 如果您在动画开始后立即将其设置为View.INVISIBLE,则动画将可见,而视图将在动画停止时消失。 我认为您代码的问题可能是您使用了GONE而不是INVISIBLE。 第二个问题可能是您在设置监听器之前启动了动画,但是使用我的解决方案实际上不需要任何监听器。 此外,请取消FillAfter选项。
 TranslateAnimation animate = new TranslateAnimation(0,-rlImages.getWidth()/2,0,0);
 animate.setDuration(1000);
 centre_leftanimate.startAnimation(animate);
 centre_leftanimate.setVisibility(View.INVISIBLE);

我尝试在onAnimationEnd中使用Invisible,但仍然无法正常工作。我的动画视图位于动画结束的同一位置。我希望在动画结束后将移动动画视图设置为不可见。 - user1891910
1
你移除了 setFillAfter(true) 吗? - Beppi's

0

我曾经遇到过同样的问题,后来通过这个问题的答案Android - remove View when its animation finished解决了这个问题。

编辑: 你可以像这样做:

final MenuActivity MN= MenuActivity.this;
    AFade.setAnimationListener(new Animation.AnimationListener(){
            @Override
            public void onAnimationStart(Animation arg0) {
            }           
            @Override
            public void onAnimationRepeat(Animation arg0) {
            }           
            @Override
            public void onAnimationEnd(Animation arg0) {
                LayoutMain.post(new Runnable() {
                    public void run () {
                        // it works without the runOnUiThread, but all UI updates must 
                        // be done on the UI thread
                        MN.runOnUiThread(new Runnable() {
                            public void run() {
                                LayoutMain.removeView(NextButton);
                            }
                        });
                    }
                });
            }
        });

LayoutMain是包含视图的布局。 MN是您的活动。


你能再解释一下吗? - Dieter Meemken

0

只需在onAnimationEnd内调用即可

animate.setListener(null)

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