5

I am trying to use animation while replacing fragments (with replace() of fragment transaction). I set animtaions with setCustomAnimations(). My animation is trying to overlap an old fragment with a new one moving from right. But the problem is: I can see views from old fragment even when it is already overlapped by the new one. And old views dissapear only when the animation is done. Also, when a new fragment is a complicated one (listviews, etc...), I can see some artefacts and flickering while overlapping. This is terrible, how can i avoid that?

My code for enter animation:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:ordering="together">


  <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:propertyName="x" 
    android:valueType="floatType"
    android:valueTo="0" 
    android:valueFrom="720"
    android:duration="250"
    android:zAdjustment="top"/>  

</set>

UPDATE: about artefacts - it's not them. It was just the consequences of bad overlay. I watched animation slowly. So the problem is: the old fragment stays on TOP. It completely overlaps the new one while animating.

Ravi
  • 34,851
  • 21
  • 122
  • 183
T.Vert
  • 279
  • 2
  • 13
  • Possible duplicate of [ugly fragment transition to surfaceview with overlay](http://stackoverflow.com/questions/14925060/ugly-fragment-transition-to-surfaceview-with-overlay) – Jesus Angulo Dec 21 '15 at 14:06
  • 1
    I don't have a SurfaceView, also overlapping is bad on all the fragments, not only difficult ones (with listviews) – T.Vert Dec 21 '15 at 14:13

2 Answers2

1

If you don't have a background set on your new fragment that can cause the weird look you are talking about. If that is the case, just set a background color on the new fragment that is overlapping the first one.

android:background="@drawable/background"
Eagle11
  • 661
  • 8
  • 25
1

Your problem happens because you are using same animator file in enter and exit animation, you have to set exit animation for old fragment different from enter one, like to another side

transaction.setCustomAnimations(<enterAnimationResId>, <exitAnimationResId>);

where enterAnimationResId is a xml file containing animation to be applied on entering fragment, and exitAnimationResId is a xml file containing animation to be applied on closing the existing fragment.

Andrey Moiseev
  • 3,914
  • 7
  • 48
  • 64