Animate the Transition Between Fragments

Animate the transition between fragments

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' FragmentHideShow.java:

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="http://schemas.android.com/apk/res/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:

res/animator/slide_in.xml:

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/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.

Transition animation between fragment

Google released the new Navigation UI library

So, now we can do the same fragment transitions from a your_named_navigation.xml resource (main > res > navigation > your_named_navigation.xml),

this an snippet code of my implementation:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
app:startDestination="@+id/first_fragment">

<fragment
android:id="@+id/first_fragment"
android:name="com.yourpackage.FirstFragment"
android:label="@string/title_first"
tools:layout="@layout/fragment_first">

<action
android:id="@+id/second_fragment_action"
app:destination="@id/second_fragment"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right" />

</fragment>

<fragment
android:id="@+id/second_fragment"
android:name="com.yourpackage.SecondFragment"
android:label="@string/title_second"
tools:layout="@layout/fragment_second">

<action ...next fragment/>

</fragment>

</navigation>

it also helps to handle clicks on back button and up button,

so, after have NavigationUi implementation in our proyect, we can call from our firstFragment instance the Navigation.findNavController method

myButton.setOnClickListener(View.OnClickListener {
//This opens our second fragment creating a stack of fragments
Navigation.findNavController(it).navigate(R.id.second_fragment_action)
})

The next Google's Codelab helped me, maybe can help you, greetings

Fragment Transition Animation

As I can not make sense out of you animation file which is fromYDelta, toXDelta, which are to opposite things. So, I am considering you have meant fromYDelta toYDelta, which will make the fragment slide from bottom to top.

Now, about the answer. When setting up animations on navigation you have to create an exit animation as well as enter animation. Otherwise you'll face this problem of white screen.

So, what you can do is add the below animation as exit animation.

    <set xmlns:android="http://schemas.android.com/apk/res/android">

<translate android:fromYDelta="0%" android:toYDelta= "-100%" android:duration = "450"/>
</set>

It will make the splash fragment slide upwards as well, but slower than the fragment1. And empty white screen will not be shown.

Animate the transition between fragments in FragNav library

Finally got the answer .

setTransitionMode() is not a static method so need to be used it from an instance of the FragNavController object.

FragNavControler fragNavController = new FragNavController(savedInstanceState, getSupportFragmentManager(), R.id.container, fragments, INDEX_RECENTS);
fragNavController.setTransitionMode(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

Animate fragment transition when showing and hiding (not replacing)

The same as you would animate replace transition. The only thing you should keep in mind - animate via method with 4 parameters:

public abstract FragmentTransaction setCustomAnimations(@AnimatorRes @AnimRes int enter, @AnimatorRes @AnimRes int exit, @AnimatorRes @AnimRes int popEnter,         @AnimatorRes @AnimRes int popExit);


Related Topics



Leave a reply



Submit