Android Animation leaving artifacts
Asked Answered
Q

2

6

I'm having a rather odd problem with Animation in my project. In the screenshots posted below you can clearly see the Totals card leaving some sort of trail upon animation. This problem is reproducible on a stock Nexus 7 running 4.2.2. The Totals card has a Google Now style Animation. It is important to note that the container for the Totals card is a Fragment, the Menu to the left in the landscape screenshot is a different Fragment, and the bottom buttons are part of the Activity that the Fragments are attached to.

Landscape Screenshot

Portrait screenshot

I can't seem to find a solution online, I have tried setFillAfter"true" and offsetting the beginning of the animation. The trail in the Activity's buttons on the bottom ("Previous & "Next) disappears when one of the buttons are pressed.

Here's the code in the onCreateView of the Totals Fragment:

    final View view = inflater.inflate(R.layout.fragment_totals,
            container, false);      

    Fonts.setRobotoThinFont(getActivity(), view);

    final LinearLayout mContainer = (LinearLayout)
            view.findViewById(R.id.container);

    final View mCard = inflater
            .inflate(R.layout.view_simpletotal, mContainer, false);

    Animation animation = AnimationUtils.loadAnimation(getActivity(),
            R.anim.card_animation);

    mContainer.addView(mCard);

    mCard.startAnimation(animation);  

    return view;

Any help would be much appreciated!

Quiche answered 11/5, 2013 at 21:29 Comment(5)
Did you try to invalidate the entire activity container when the animation ends?Searching
That gets rid of it, thank you very much! Is doing this considered "good practice"?Quiche
Well I think you should better use fragment custom animations like shown in official references (it seems you are animating entire fragment, don't you?) developer.android.com/training/animation/cardflip.htmlSearching
Good point but I am animating a View inside of the Fragment not the whole Fragment itself. Thanks for the reference though!Quiche
If you don't mind I'll post the given solution as answer, so you can accept it.Searching
S
4

You can easily get rid of it by simply invalidating the container activity when the animation ends.

Searching answered 11/5, 2013 at 23:16 Comment(0)
C
3

I suggest you to use setLayoutAnimation() method of ViewGroup class. I think it will help you.
For example, you can write this code inside your fragment class :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ...
    rootView.setLayoutAnimation(getLayoutAnimation());
    ...
}
private LayoutAnimationController getLayoutAnimation() {
    Animation animation = new RotateAnimation(30, 0, -100, 0);
    animation.setDuration(1000);
    LayoutAnimationController layoutAnimationController = new LayoutAnimationController(animation);
    return layoutAnimationController;
}

See what will happen with animation.

Calvert answered 11/5, 2013 at 23:30 Comment(2)
Thanks for the reply! I'm animating a View inside of the Fragment not the Fragment itself but your answer might help me with another unrelated problem. Thanks for the input!Quiche
This example works exactly for views. You can apply this to any ViewGroup object you want. For your example you can put white block with Totals details into some ViewGroup and then apply animation which I proposed. If there will be some issues with running animation make layout params for that ViewGroup as MATCH_PARENT. You can read more here: [link]developer.android.com/reference/android/view/animation/…Calvert

© 2022 - 2024 — McMap. All rights reserved.