Kevin Li
Kevin Li

Reputation: 195

Android: Transition between two scenes

I will be making an app that can transition from one scene to another with a swipe up on the screen. Each of these scenes perform different functions, however I want the transition to be animated smoothly, as if the two scenes are connected.

Would it be better to make two activities and add transitions between them or make an activity with fragments?

Upvotes: 0

Views: 578

Answers (1)

Droid Chris
Droid Chris

Reputation: 3783

These methods should animate fragments (little more involved than this but this example should get you started :) ) -

private void animateSwipe(int layoutContainerID, Fragment fragment, String fragmentTag) {
    FragmentTransaction fragmentTransaction =  getFragmentTransactionWithAnimation(true);
    fragmentTransaction.replace(layoutContainerID, fragment, fragmentTag);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
    fragmentTransaction = null; 
}


private FragmentTransaction getFragmentTransactionWithAnimation(boolean swipeLeft) {
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

    if(swipeLeft)
        fragmentTransaction.setCustomAnimations(R.animator.animate_in, R.animator.animate_out);
    else
        fragmentTransaction.setCustomAnimations(R.animator.animate_in_from_right, R.animator.animate_out_to_left);

    return fragmentTransaction;
}

Here is the XML animation files (put in folder called animator in res folder -

animate_in.xml -

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

animate_out.xml -

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

animate_out_to_left.xml -

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

animate_in_from_right.xml -

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

Upvotes: 1

Related Questions