android - Animate the transition between fragments

ID : 10376

viewed : 19

Tags : androidandroid-3.0-honeycombandroid

Top 5 Answer for android - Animate the transition between fragments

vote vote


You need to use the new android.animation framework (object animators) with FragmentTransaction.setCustomAnimations as well as FragmentTransaction.setTransition.

Here's an example on using setCustomAnimations from ApiDemos'

ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out); 

and here's the relevant animator XML from res/animator/fade_in.xml:

<objectAnimator xmlns:android=""     android:interpolator="@android:interpolator/accelerate_quad"     android:valueFrom="0"     android:valueTo="1"     android:propertyName="alpha"     android:duration="@android:integer/config_mediumAnimTime" /> 

Note that you can combine multiple animators using <set>, just as you could with the older animation framework.

EDIT: Since folks are asking about slide-in/slide-out, I'll comment on that here.

Slide-in and slide-out

You can of course animate the translationX, translationY, x, and y properties, but generally slides involve animating content to and from off-screen. As far as I know there aren't any transition properties that use relative values. However, this doesn't prevent you from writing them yourself. Remember that property animations simply require getter and setter methods on the objects you're animating (in this case views), so you can just create your own getXFraction and setXFraction methods on your view subclass, like this:

public class MyFrameLayout extends FrameLayout {     ...     public float getXFraction() {         return getX() / getWidth(); // TODO: guard divide-by-zero     }      public void setXFraction(float xFraction) {         // TODO: cache width         final int width = getWidth();         setX((width > 0) ? (xFraction * width) : -9999);     }     ... } 

Now you can animate the 'xFraction' property, like this:


<objectAnimator xmlns:android=""     android:interpolator="@android:anim/linear_interpolator"     android:valueFrom="-1.0"     android:valueTo="0"     android:propertyName="xFraction"     android:duration="@android:integer/config_mediumAnimTime" /> 

Note that if the object you're animating in isn't the same width as its parent, things won't look quite right, so you may need to tweak your property implementation to suit your use case.

vote vote


I have done this way:

Add this method to replace fragments with Animations:

public void replaceFragmentWithAnimation( fragment, String tag){     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();     transaction.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);     transaction.replace(, fragment);     transaction.addToBackStack(tag);     transaction.commit(); } 

You have to add four animations in anim folder which is associate with resource:


<?xml version="1.0" encoding="utf-8"?> <set xmlns:android=""     android:shareInterpolator="false">     <translate         android:fromXDelta="-100%" android:toXDelta="0%"         android:fromYDelta="0%" android:toYDelta="0%"         android:duration="700"/> </set> 


<?xml version="1.0" encoding="utf-8"?> <set xmlns:android=""     android:shareInterpolator="false">     <translate         android:fromXDelta="0%" android:toXDelta="100%"         android:fromYDelta="0%" android:toYDelta="0%"         android:duration="700" /> </set> 


<?xml version="1.0" encoding="utf-8"?> <set xmlns:android=""     android:shareInterpolator="false">     <translate         android:fromXDelta="100%" android:toXDelta="0%"         android:fromYDelta="0%" android:toYDelta="0%"         android:duration="700" /> </set> 


<?xml version="1.0" encoding="utf-8"?> <set xmlns:android=""     android:shareInterpolator="false">     <translate         android:fromXDelta="0%" android:toXDelta="-100%"         android:fromYDelta="0%" android:toYDelta="0%"         android:duration="700"/> </set> 


enter image description here

Its Done.

vote vote


If you can afford to tie yourself to just Lollipop and later, this seems to do the trick:

import android.transition.Slide; import android.util.Log; import android.view.Gravity; . . . f = new MyFragment(); f.setEnterTransition(new Slide(Gravity.END)); f.setExitTransition(new Slide(Gravity.START)); getFragmentManager()     .beginTransaction()     .replace(, f, FRAG_TAG)  // FRAG_TAG is the tag for your fragment     .commit(); 

Kotlin version:

f = MyFragment().apply {     enterTransition = Slide(Gravity.END)     exitTransition = Slide(Gravity.START) } fragmentManager     .beginTransaction()     .replace(, f, FRAG_TAG)  // FRAG_TAG is the tag for your fragment     .commit(); 

Hope this helps.

vote vote


Nurik's answer was very helpful, but I couldn't get it to work until I found this. In short, if you're using the compatibility library (eg SupportFragmentManager instead of FragmentManager), the syntax of the XML animation files will be different.

vote vote


Here's a slide in/out animation between fragments:

FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.setCustomAnimations(R.animator.enter_anim, R.animator.exit_anim); transaction.replace(, new YourFragment()); transaction.commit(); 

We are using an objectAnimator.

Here are the two xml files in the animator subfolder.


<?xml version="1.0" encoding="utf-8"?> <set>      <objectAnimator          xmlns:android=""          android:duration="1000"          android:propertyName="x"          android:valueFrom="2000"          android:valueTo="0"          android:valueType="floatType" /> </set> 


<?xml version="1.0" encoding="utf-8"?> <set>     <objectAnimator         xmlns:android=""         android:duration="1000"         android:propertyName="x"         android:valueFrom="0"         android:valueTo="-2000"         android:valueType="floatType" /> </set> 

I hope that would help someone.

Top 3 video Explaining android - Animate the transition between fragments