1

I am trying to move from one Fragment to another with performing a 3D flip animation. To accomplish this, I am trying to adapt the Google tutorial here to my context. The only thing is that the guide implement both Fragments in an Activity while I implement the Fragments separately in two files without relying on Activity.

The issue I am encountering is the following runtime exception whenever I try to animate the transition:

java.lang.RuntimeException: Unknown animation name: objectAnimator

Here is the approach I followed:

1- In build.gradle file, the minimum SDK supported version is 14:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"

    defaultConfig {
        applicationId "com.xxxxx.xxxxxxxx"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

2- Under the res folder, I created a new Android resource directory anim: enter image description here

The following is the code of the 4 xml files:

card_flip_left_in.xml:

<!-- Rotate. -->
<objectAnimator
    android:valueFrom="-180"
    android:valueTo="0"
    android:propertyName="rotationY"
    android:interpolator="@android:interpolator/accelerate_decelerate"
    android:duration="@integer/card_flip_time_full" />

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

card_flip_left_out.xml:

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Rotate. -->
    <objectAnimator
        android:valueFrom="0"
        android:valueTo="180"
        android:propertyName="rotationY"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:duration="@integer/card_flip_time_full" />

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

card_flip_right_in.xml:

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

    <!-- Rotate. -->
    <objectAnimator
        android:valueFrom="180"
        android:valueTo="0"
        android:propertyName="rotationY"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:duration="@integer/card_flip_time_full" />

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

</set>

card_flip_right_out.xml:

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Rotate. -->
    <objectAnimator
        android:valueFrom="0"
        android:valueTo="-180"
        android:propertyName="rotationY"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:duration="@integer/card_flip_time_full" />

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

Now come the interesting part, I try to perform the transition whenever on click on the marker info window:

@Override
    public void onInfoWindowClick(Marker marker) {
        System.out.println(marker.getId());
        flipCard();
    }

And the flipCard code:

private void flipCard() {

        if (mShowingBack) {
            getFragmentManager().popBackStack();
            return;
        }
        // Flip to the back.

        mShowingBack = true;
        // Create and commit a new fragment transaction that adds the fragment for the back of
        // the card, uses custom animations, and is part of the fragment manager's back stack.

        getFragmentManager()
                .beginTransaction()

                        // Replace the default fragment animations with animator resources representing
                        // rotations when switching to the back of the card, as well as animator
                        // resources representing rotations when flipping back to the front (e.g. when
                        // the system Back button is pressed).
                .setCustomAnimations(
                        R.anim.card_flip_right_in , R.anim.card_flip_right_out,
                        R.anim.card_flip_left_in, R.anim.card_flip_left_out
                )

                        // Replace any fragments currently in the container view with a fragment
                        // representing the next page (indicated by the just-incremented currentPage
                        // variable).
                .replace(R.id.audio_playback, new AudioPlayback())

                        // Add this transaction to the back stack, allowing users to press Back
                        // to get to the front of the card.
                .addToBackStack(null)

                        // Commit the transaction.
                .commit();
}

One of the attempts I tried:

I tried to move all the card animation xml files to an animator resource directory instead of an anim directory. But then whenever I change the path in the transition code to the following:

.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
                )

I got the statements above underlined in red with an error Expected resource of type anim. So this is to confirm this attempt doesn't seems to fix the issue.

So basically, how is the flip animation should be done? What I am missing here?

Malloc
  • 15,434
  • 34
  • 105
  • 192

1 Answers1

0

Looks like you are using the compatibility library which means that your resources should be in a folder called res/anim rather than res/animator and use the older syntax.

The syntax you are using is described under Property Animation here. You should be using the syntax described under View Animation here

Creating slide, fade, translate or scale transitions with this syntax is not difficult but rotation is only about the X and / or Y axes and I have yet to see a successful implementation of a flip transition. See here for one attempt.

Community
  • 1
  • 1
eggdeng
  • 148
  • 9