Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove an animated view after animation ends in Android?

I am using an right to left moving animation for a RelativeLayout.

I tried to set the Visibility to 'GONE' for the Layout in the onAnimationEnd(),but it is not working. The animated view is still there in the place where it stops.

This is the code I used:

Creating animation for right to left:

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

Setting the animation to layout:

centre_leftanimate.startAnimation(animate);

Adding listeners to animation:

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

How to make the visibility of animated view to be invisible after the end of animation?

Please suggest.

like image 902
user1891910 Avatar asked Oct 30 '13 11:10

user1891910


People also ask

How do I turn off infinite animation on Android?

Use clearAnimation() to stop an animation.

How do you remove animated pictures?

Press and hold CTRL, and then in the Animation Task pane, select each animation effect that you want to remove, right-click one of the selected effects and select Remove.

How do I remove an effect from an animated object?

On the Animation tab, click Animation Pane. On the slide, click the animated object that you want to remove the effect from. Tip: All of the effects applied to that object are highlighted in the Animation Pane.

What is animation in Android and how to use it?

Animation is the process of adding a motion effect to any view, image, or text. With the help of an animation, you can add motion or can change the shape of a specific view. Animation in Android is generally used to give your UI a rich look and feel. The animations are basically of three types as follows: 1. Property Animation

How to show and hide a view with slide up/down animation in Android?

This example demonstrate about how to Show and hide a View with a slide up/down animation in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

How to turn off animation in AutoCAD?

On the Animations tab, in the Advanced Animation group, click Animation Pane. In the Animation Pane, right-click the animation effect that you want to remove and then click Remove. Note: You can also remove multiple, specific animation effects.


4 Answers

I had the very same issue, except I actually removed the view being animated, but the animation persisted!

The simple solution is to actually cancel the animation, then you can hide or remove the view.

animatedView.getAnimation().cancel();

or if you still have your animation object:

animation.cancel();

EDIT: There seem to be some left over with the above solution, so I added this:

animatedView.setAnimation(null);
like image 53
3c71 Avatar answered Oct 13 '22 19:10

3c71


I had the same problem and in my case removing

android:fillAfter="true"

in the animation responsible for removing view and this code solve my problem. But others will have better code.

viewToHide.startAnimation(animationForHide);
viewToHide.setVisibility(View.GONE);
like image 40
KaungMyatMin Avatar answered Oct 13 '22 19:10

KaungMyatMin


I know this is an old question but I encountered this issue today and I wanted to show how I solved it, because although the answers already posted helped, none of them was perfectly working for my case.

In my case I created a custom view dinamically, applied 2 animations (alpha and translation) and removed the view after the animations were done. This is how I did it:

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

Note that this was done inside a custom view (which extended a FrameLayout), everything runs on the UI thread.

like image 22
Synx Avatar answered Oct 13 '22 19:10

Synx


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);
like image 27
Beppi's Avatar answered Oct 13 '22 19:10

Beppi's