【问题标题】:Returning view rotation to it's original state after canceling animation in API < 11在 API < 11 中取消动画后将视图旋转返回到其原始状态
【发布时间】:2016-09-01 19:05:39
【问题描述】:

我正在视图上运行摇动动画:

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="100"
    android:fromDegrees="-5"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:toDegrees="5" />

我正在根据用户需求取消动画,但视图保持旋转,就像停止时一样,我不希望它恢复到原来的状态。

这是我停止动画的方式:

    if (animation != null) {
        animation.cancel();
        animation.reset();
    }

我不能使用setRotation(),因为我的最小 API 是 9。

我怎样才能做到这一点?

【问题讨论】:

    标签: android android-animation


    【解决方案1】:

    试试view.clearAnimation()方法,它会清除视图上的所有动画效果

    【讨论】:

      【解决方案2】:

      你试过setRotation(0,0f);吗?

      编辑: 我明白了,这需要 API 11,而您需要 API 9 及更高版本。 (在 2014 年支持这么低的 API 几乎没有什么理由,但是好吧,我们并不总是能规定这一点。)也许是 Animation 的子类,这样当你“取消”它时,它只会动画到 0 的旋转,然后调用 super。取消()。

      【讨论】:

      • 由于 API 低,我不能
      • 我明白了。 (很少有理由支持这么低的 API,但没关系。)也许是 Animation 的子类,这样当你“取消”它时,它只会动画到 0 的旋转,然后调用 super.cancel()...
      【解决方案3】:

      我用了这个方法(代码在继承自View的类里面):

      private void returnToOriginalRotationState() {
          RotateAnimation animation = new RotateAnimation(0.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
          animation.setInterpolator(new LinearInterpolator());
          animation.setDuration(1);
          animation.setFillAfter(true);
      
          startAnimation(animation);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-08
        相关资源
        最近更新 更多