Animate the transition between fragments

前端 未结 8 1086
梦如初夏
梦如初夏 2020-11-22 14:59

I\'m trying to animate the transition between fragments. I got the answer from the following
Android Fragments and animation

FragmentTransaction ft = g         


        
相关标签:
8条回答
  • 2020-11-22 15:03

    Try using this simple and fasted solution. Android provides some default animations.

    fragmentTransaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    fragmentManager.addOnBackStackChangedListener(this);
    fragmentTransaction.replace(R.id.frame, firstFragment, "h");
    fragmentTransaction.addToBackStack("h");
    fragmentTransaction.commit();
    

    Output:

    0 讨论(0)
  • 2020-11-22 15:10

    Android SDK implementation of FragmentTransaction wants an Animator while support library wants an Animation, don't ask me why but after strangeluk's comment I looked into android 4.0.3 code and support library. Android SDK uses loadAnimator() and support library uses loadAnimation()

    0 讨论(0)
  • 2020-11-22 15:15

    I have done this way:

    Add this method to replace fragments with Animations:

    public void replaceFragmentWithAnimation(android.support.v4.app.Fragment 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(R.id.fragment_container, fragment);
        transaction.addToBackStack(tag);
        transaction.commit();
    }
    

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

    enter_from_left.xml:

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

    exit_to_right.xml:

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

    enter_from_right.xml:

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

    exit_to_left.xml:

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

    Output:

    Its Done.

    0 讨论(0)
  • 2020-11-22 15:15

    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(R.id.listFragment, new YourFragment());
    transaction.commit();
    

    We are using an objectAnimator.

    Here are the two xml files in the animator subfolder.

    enter_anim.xml

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

    exit_anim.xml

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

    I hope that would help someone.

    0 讨论(0)
  • 2020-11-22 15:17

    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.

    0 讨论(0)
  • 2020-11-22 15:20

    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.

    0 讨论(0)
提交回复
热议问题