21

I am currently reading this tutorial:

http://developer.android.com/training/animation/cardflip.html

on flip Animations of Fragments. Unfortunately, the object-animator is only available for android.app.Fragment, and not the support Fragment.

I tried to reconstruct the .xml animations using scale and rotation animations. But now the animations are just not executed, and after the time that I've set in the animations .xml file passes, the other Fragment appears, instead of flipping.

  • Did I simply make a misstake in implementing the .xml animations?
  • Or is it not possible to do a 3D flip animation without object-animator?
  • Or is it not possible to do a 3D flip animation with the support Fragment?

Here are my .xml animations: flip_left_in.xml

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

  <!-- Before rotating, immediately set the alpha to 0. -->
 <alpha
    android:valueFrom="1.0"
    android:valueTo="0.0"
    android:propertyName="alpha"
    android:duration="0" />

 <!-- Rotate. -->
 <rotate
    android:valueFrom="-180"
    android:valueTo="0"
    android:propertyName="rotationY"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:duration="800"/>

<!-- Half-way through the rotation (see startOffset), set the alpha to 1. -->
<alpha
    android:valueFrom="0.0"
    android:valueTo="1.0"
    android:startOffset="400"
    android:duration="1" /> 
</set>

flip_left_out.xml

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

   <!-- Rotate. -->
   <rotate
    android:duration="800"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:propertyName="rotationY"
    android:valueFrom="0"
    android:valueTo="180" />

<!-- Half-way through the rotation (see startOffset), set the alpha to 0. -->
<alpha
    android:duration="1"
    android:propertyName="alpha"
    android:startOffset="400"
    android:valueFrom="1.0"
    android:valueTo="0.0" />

 </set>

flip_right_in.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Before rotating, immediately set the alpha to 0. -->
<alpha
    android:duration="0"
    android:propertyName="alpha"
    android:valueFrom="1.0"
    android:valueTo="0.0" />

<!-- Rotate. -->
<rotate
    android:duration="800"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:propertyName="rotationY"
    android:valueFrom="180"
    android:valueTo="0" />

<!-- Half-way through the rotation (see startOffset), set the alpha to 1. -->
<alpha
    android:duration="1"
    android:propertyName="alpha"
    android:startOffset="400"
    android:valueFrom="0.0"
    android:valueTo="1.0" />

  </set>

flip_right_out.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Rotate. -->
<rotate
    android:duration="800"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:propertyName="rotationY"
    android:valueFrom="0"
    android:valueTo="-180" />

<!-- Half-way through the rotation (see startOffset), set the alpha to 0. -->
<alpha
    android:duration="1"
    android:propertyName="alpha"
    android:startOffset="400"
    android:valueFrom="1.0"
    android:valueTo="0.0" />

 </set>

And here is the code where they are executed:

FragmentTransaction trans = getActivity().getSupportFragmentManager().beginTransaction();

trans.setCustomAnimations(R.anim.flip_right_in, R.anim.flip_right_out, 
                           R.anim.flip_left_in, R.anim.flip_left_out);
trans.addToBackStack(null);

trans.replace(R.id.content_frame, new MyFragment()).commit();
LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • Did you made 3d flip effect? can you please tell me how you've replaced the above code with object animator? –  Sep 26 '16 at 12:50

3 Answers3

3

You can use NineOldAndroids. It backports the Honeycomb (Android 3.0) animation API all the way back to Android 1.0. You'll get ObjectAnimator, ValueAnimator and all the other good stuff.

Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
2

Thank you all for your help.

I managed to solve my problem. The solution has to do with NineOldAndroids and another Library with support-v4 support for NineOldAndroids.

What I did:

  • I downloaded this library: https://github.com/kedzie/Support_v4_NineOldAndroids (This is a support library for NineOldAndroids)
  • Imported it into my workspace
  • Downloaded the NineOldAndroids Library and imported it into my workspace
  • Imported the NineOldAndroids library into the support-v4 Library
  • Imported the support-v4-nineoldandroids library into my project
  • Did the Filp-Animation
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • If smb have issues with "support-v4 support for NineOldAndroids" importing etc. - make sure your ADT is up to date. Like if this support-v4 built against API 19 it has to be available in your ADT. I had issues cuz my ADT had only API 18. The solution was to update ADT to the latest version. – Stan Apr 20 '14 at 15:14
  • how to do with support-v4 support for NineOldAndroids using android studio – Sathish Kumar J Jun 13 '17 at 12:27
1

In case if you are not supporting below api<3

use the same code as given in: https://stuff.mit.edu/afs/sipb/project/android/docs/training/animation/cardflip.html

just tweaked the flipCard method to:

private void flipCard() {
if (mShowingBack) {
    mShowingBack = false;
    FragmentTransaction trans = getActivity().getFragmentManager().beginTransaction();
    trans.setCustomAnimations(R.animator.card_flip_right_in,
            R.animator.card_flip_right_out,
            R.animator.card_flip_left_in,
            R.animator.card_flip_left_out)
         .replace(R.id.memberCardContainer, new CardFrontFragment())
         .commit();
    return;
}

// Flip to the back.
mShowingBack = true;
FragmentTransaction trans = getActivity().getFragmentManager().beginTransaction();
trans.setCustomAnimations(R.animator.card_flip_right_in,
        R.animator.card_flip_right_out,
        R.animator.card_flip_left_in,
        R.animator.card_flip_left_out)
     .replace(R.id.memberCardContainer, new CardBackFragment())
     .commit();
}
Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
ReeSen
  • 75
  • 4
  • This gives a conversion error when your fragment classes extend the support v4 fragment – Eli Aug 06 '17 at 16:03