简体   繁体   中英

Android Fragment Translate Animation Not Working

I'm trying to do a simple translate animation for two fragments. One comes in from the right while the other goes out to the left. My min SDK is 14. What happens is the transition takes place, but without the actual animation. After the time specified in the animation xml, the fragments just swap. So the animation time is being respected, but there is no actual translation.

My fragment animation code is:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.animator.enter_from_right, R.animator.exit_to_left);
fragmentTransaction.replace(android.R.id.content, termsFragment);
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();

My animation xml files are (enter_from_right):

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXDelta="100%"
    android:toXDelta="0%"
    android:duration="1000" />
</set>

and exit_to_left:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXDelta="0%"
    android:toXDelta="-100%"
    android:duration="1000"/>
</set>

Looks like you are confusing two animation frameworks, android.animation and android.view.animation (yes, it's confusing).

Your XML animation is kind of a mixture between the two. Either you set an android.animation.ObjectAnimator or a android.view.animation.Animation : see here and here for reference. In this particular case I think you are looking for a simple translate animation, which belongs to the latter, older, simpler class (and link).

So:

  • change <objectAnimator> tag to <translate> ;
  • move your file from the animator folder to the anim resource folder, and recall it using R.anim .

I recommend reading the official documentation linked which is very clear on this topic. Basically, for simple translation / rotation / alpha animation, it's better to use view animations ( <translate>, <rotate>, <scale>, <alpha> ) in res/anim folder.

Property animation (like <objectAnimator> in res/animator) are a more powerful tool that you would rather use for complex situations.


With some research I found that setCustomAnimations() has an even more confusing behaviour.

If you are using support libraries, setCustomAnimations() only accepts simple animation objects (like your <translate> ). In that case it all should work, you just have to change getFragmentManager() to getSupportFragmentManager() .

If you are not using support libraries, setCustomAnimations() only accepts property animations (like the <objectAnimator> ).

In this second case your simple animation becomes quite hard to do (see here and here for reference).

You can:

  • switch to support libraries, which can be boring if your development is not at an early stage, and use the support fragment manager;
  • do some work about objectAnimators and translations - there are lots of questions about, I just linked one above;
  • use one of the default transitions:

     FragmentTransaction t = getFragmentManager().beginTransaction(); t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); or t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); or t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); or 
  • try with a very bad <objectAnimator> in your res/animator resource folder:

     <objectAnimator android:propertyName="translationX" android:duration="1000" android:valueFrom="1000" android:valueTo="0" android:valueType="floatType"/> 

This is bad because you need to specify a value in pixels (here I put 1000), so that will look different on different devices. But maybe for a fast translation that's not a real problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM