Я пытаюсь перейти от одного фрагмента к другому, выполняя трехмерную анимацию переворота. Для этого я пытаюсь адаптировать руководство Google здесь в моем контексте. Единственное, что в руководстве реализованы оба фрагмента в Activity
, а я реализую фрагменты отдельно в двух файлах, не полагаясь на Activity
.
Проблема, с которой я сталкиваюсь, заключается в следующем исключении времени выполнения всякий раз, когда я пытаюсь анимировать переход:
java.lang.RuntimeException: Unknown animation name: objectAnimator
Вот подход, которого я придерживался:
1- В файле build.gradle
минимальная поддерживаемая версия SDK - 14:
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "com.xxxxx.xxxxxxxx"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
2- В папке res я создал новый каталог ресурсов Android anim
:
Ниже приведен код 4 файлов xml:
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>
А теперь самое интересное: я пытаюсь выполнять переход всякий раз, когда нажимаю на информационное окно маркера:
@Override
public void onInfoWindowClick(Marker marker) {
System.out.println(marker.getId());
flipCard();
}
И код флип-карты:
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();
}
Одна из моих попыток:
Я попытался переместить все xml-файлы анимации карточек в каталог ресурсов animator
вместо каталога anim
. Но затем всякий раз, когда я меняю путь в коде перехода на следующий:
.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
)
Я получил утверждения выше, подчеркнутые красным, с ошибкой Expected resource of type anim
. Итак, это подтверждает, что эта попытка, похоже, не решает проблему.
Итак, в основном, как должна выполняться анимация переворота? Что мне здесь не хватает?