16

我正在为RelativeLayout 使用从右到左移动的动画。

我尝试将 中的 Layout 的 Visibility 设置为“GONE” onAnimationEnd(),但它不起作用。动画视图仍然存在于它停止的地方。

这是我使用的代码:

从右到左创建动画:

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);
    }
});

如何在动画结束后使动画视图的可见性不可见?

请建议。

4

6 回答 6

24

我遇到了同样的问题,除了我实际上删除了正在动画的视图,但动画仍然存在!

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

animatedView.getAnimation().cancel();

或者如果你还有你的动画对象:

animation.cancel();

编辑:上面的解决方案似乎还有一些剩余,所以我添加了这个:

animatedView.setAnimation(null);
于 2014-06-19T17:14:33.590 回答
2

我有同样的问题,在我的情况下删除

android:fillAfter="true"

在负责删除视图的动画中,此代码解决了我的问题。但其他人会有更好的代码。

viewToHide.startAnimation(animationForHide);
viewToHide.setVisibility(View.GONE);
于 2016-02-09T12:36:28.707 回答
2

我知道这是一个老问题,但我今天遇到了这个问题,我想展示我是如何解决它的,因为虽然已经发布的答案有所帮助,但没有一个完全适合我的案例。

在我的例子中,我动态地创建了一个自定义视图,应用了 2 个动画(alpha 和翻译)并在动画完成后删除了视图。我是这样做的:

//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 线程上运行。

于 2017-02-09T03:25:49.850 回答
1

You can set the Visibility of your View when you want. If you set it to View.INVISIBLE right after the start of the animation, the animation will be visible, and the View will disappear as soon as the animation stops. I think that the problem with your code could be that you are using GONE instead of INVISIBLE. The second problem might be that you start the animation before setting the listener, but with my solution you don't need actually any listener. Also, take away the FillAfter option.

 TranslateAnimation animate = new TranslateAnimation(0,-rlImages.getWidth()/2,0,0);
 animate.setDuration(1000);
 centre_leftanimate.startAnimation(animate);
 centre_leftanimate.setVisibility(View.INVISIBLE);
于 2013-10-30T12:08:49.490 回答
0

只需在 onAnimationEnd 内部调用

animate.setListener(null)
于 2019-02-12T15:30:26.313 回答
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 是您的活动。

于 2016-04-14T07:56:13.767 回答