【问题标题】:ViewAnimator using rotate anim to transition between viewsViewAnimator 使用旋转动画在视图之间转换
【发布时间】:2011-10-06 16:23:53
【问题描述】:

我想使用 ViewAnimator 从一个视图转换到另一个视图(在我的测试应用程序中,视图是 TextView)。下面列出了我的两个动画。我看到的行为是两个动画在我触发动画器后立即开始,而不是让 InAnimation 运行,一旦它完成了 OutAnimation 运行。我所看到的看起来像一个风车——向外旋转的视图垂直于向内旋转的视图。我希望​​看到向外旋转的视图从正常的水平位置(0 度)变为垂直(90 度);然后我想看到视图从垂直(-90 度)旋转到水平(0 度)。

@anim/rotate_out.xml

<?xml version="1.0" encoding="UTF-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0" android:toDegrees="90" android:pivotX="50%"
    android:pivotY="50%" android:repeatCount="0" android:duration="500"
    android:interpolator="@android:anim/linear_interpolator">
</rotate>

@anim/rotate_in.xml

<?xml version="1.0" encoding="UTF-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="-90" android:toDegrees="0" android:pivotX="50%"
    android:pivotY="50%" android:repeatCount="0" android:duration="500"
    android:interpolator="@android:anim/linear_interpolator">
</rotate>

在主活动 onCreate...

va = (ViewAnimator) findViewById(R.id.ViewFlipper01);
va.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_in));
va.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_out));

有什么想法吗?

【问题讨论】:

    标签: android android-layout view android-animation


    【解决方案1】:

    也许? @anim/rotate_outin.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
      <rotate xmlns:android="http://schemas.android.com/apk/res/android"
          android:fromDegrees="0" android:toDegrees="90" android:pivotX="50%"
          android:pivotY="50%" android:repeatCount="0" android:duration="500"
          android:interpolator="@android:anim/linear_interpolator">
      </rotate>
      <rotate xmlns:android="http://schemas.android.com/apk/res/android"
          android:fromDegrees="0" android:toDegrees="90" android:pivotX="50%"
          android:pivotY="50%" android:repeatCount="0" android:duration="500"
          android:interpolator="@android:anim/linear_interpolator">
      </rotate>
    </set>
    

    【讨论】:

    • 现在只有一个动画 xml 文件,我应该为 InAnimation 和 OutAnimation 设置什么?
    【解决方案2】:

    ViewAnimator的源码可以在here找到。根据那里的showOnly() 方法,有意使两个动画并行启动,因此当一个视图“移出”时,另一个视图已经“移入”。 因此,为了实现您的目标,您必须在动画中添加一些延迟,以便在动画已经完成时开始。你可以设置例如android:duration="500" 用于外动画, android:startOffset="500" 用于动画。只需确保两者的值相同即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      相关资源
      最近更新 更多