翻转两个片段之间的过渡动​​画

时间:2016-06-19 16:32:37

标签: android android-fragments animation flip

我正在尝试通过执行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资源目录animenter image description here

以下是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。所以这是为了证实这种尝试似乎无法解决问题。

所以基本上,翻转动画应该如何完成?我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

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

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

下描述的语法

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