【发布时间】:2016-10-20 22:29:42
【问题描述】:
我正在尝试通过执行 3D 翻转动画从一个片段移动到另一个片段。为了做到这一点,我正在尝试将 Google 教程 here 改编为我的上下文。唯一的问题是该指南在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();
}
还有 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。因此,这是为了确认此尝试似乎无法解决问题。
那么基本上,翻转动画应该怎么做呢?我在这里缺少什么?
【问题讨论】:
标签: android android-fragments animation flip