【问题标题】:Flip transition animation between two Fragments在两个 Fragment 之间翻转过渡动画
【发布时间】: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


    【解决方案1】:

    看起来您正在使用兼容性库,这意味着您的资源应该位于名为 res/anim 而不是 res/animator 的文件夹中,并使用较旧的语法。

    您使用的语法在属性动画here 下进行了描述。您应该使用查看动画here 中描述的语法

    使用这种语法创建幻灯片、淡入淡出、平移或缩放过渡并不困难,但旋转仅关于 X 和/或 Y 轴,我还没有看到翻转过渡的成功实现。请参阅here 进行一次尝试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 1970-01-01
      相关资源
      最近更新 更多