使用支持片段管理器翻转动画

时间:2017-07-20 00:39:40

标签: android android-fragments android-animation

所以我试图复制这个:https://developer.android.com/training/animation/cardflip.html

但是,我试图使用支持FragmentManger而不是香草FragmentManager

这就是我的目标:

为了使其适用于支持Fragment,我制作了anim个文件而不是animator个文件。

card_flip_left_in.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">


 <!-- Before rotating, immediately set the alpha to 0. -->
  <alpha
      android:valueFrom="1.0"
      android:valueTo="0.0"
      android:propertyName="alpha"
      android:duration="0" />

  <!-- Rotate. -->
  <rotate
      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. -->
  <alpha
      android:valueFrom="0.0"
      android:valueTo="1.0"
      android:propertyName="alpha"
      android:startOffset="@integer/card_flip_time_half"
      android:duration="1" />

</set>

card_flip_left_out.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
  <!-- Rotate. -->
  <rotate
      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. -->
  <alpha
      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

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

  <!-- Rotate. -->
  <rotate
      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. -->
  <alpha
      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

<set xmlns:android="http://schemas.android.com/apk/res/android">
  <!-- Rotate. -->
  <rotate
      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. -->
  <alpha
      android:valueFrom="1.0"
      android:valueTo="0.0"
      android:propertyName="alpha"
      android:startOffset="@integer/card_flip_time_half"
      android:duration="1" />
</set>

最后这是我进行转换的代码:

private void flipFragment(Fragment fragment, String tag){
    // if we are showing the back, just pop the back stack
    if(showingBack){
      getSupportFragmentManager().popBackStack();
      showingBack = false;
      return;
    }

    // otherwise flip it
    showingBack = true;
    getSupportFragmentManager()
        .beginTransaction()
        .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(getFragmentViewId(), fragment, tag)
        .addToBackStack(null)
        .commit();

  }

除动画外,一切正常。

修改 使用淡入淡出动画效果很好:

.setCustomAnimations(
        android.R.anim.fade_in, android.R.anim.fade_out,
        android.R.anim.fade_in, android.R.anim.fade_out)

1 个答案:

答案 0 :(得分:1)

您的动画无效,因为您没有使用正确的属性。您需要将valueFromvalueTo替换为tween animations的相应attrs。

<强>α

valueFrom变为fromAlpha

valueTo变为toAlpha

轮换

valueFrom变为fromDegrees

valueTo变为toDegrees

您还需要添加pivotXpivotY个attrs。

轮换y

pivotX="50%"

pivotY="0%"