如何从停止的值启动Android动画?

eli*_*tht 5 animation android

我有一个图像视图,我在其上应用旋转动画.动画效果很好.在触摸下,我尝试使用cancel()停止旋转动画,使用reset()重置动画并使用clearAnimation()清除视图上的动画.但动画又恢复了原来的位置.如何使用触发事件发生时的值停止动画并从停止的位置重新启动?

我的动画在xml中定义如下

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="360"
    android:toDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fillAfter="true"
    android:fillEnabled="true"
    android:duration="200000"
    android:repeatMode="restart"
    android:repeatCount="infinite"
    android:interpolator="@android:anim/linear_interpolator"/>
Run Code Online (Sandbox Code Playgroud)

我试图使用以下代码停止动画

private void stopAnimation(){
        mRotateAntiClockwiseAnimation.cancel();
    mRotateAntiClockwiseAnimation.reset();
    imageView.clearAnimation();
    mRotateAntiClockwiseAnimator.end();
    mRotateAntiClockwiseAnimator.cancel();
    stopAnimationForImageButton();
    }
Run Code Online (Sandbox Code Playgroud)

我使用以下代码在视图上设置动画

mRotateAntiClockwiseAnimation = AnimationUtils.loadAnimation(context, R.anim.rotate_anticlockwise);
        mRotateAntiClockwiseAnimation.setFillEnabled(true);
        mRotateAntiClockwiseAnimation.setFillAfter(true);
        imageView.setAnimation(mRotateAntiClockwiseAnimation);
        mRotateAntiClockwiseAnimation.startNow();
        imageView.invalidate();
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,即使使用cancel()或reset()也无助于在动画被触及的位置停止动画.任何指针都会有所帮助

eli*_*tht 4

我想我是通过启动动画师然后设置 currentPlayTime() 来使其工作的。文档清楚地告诉(我刚刚偶然发现),如果动画尚未开始,使用此方法设置的 currentPlayTime 将不会向前推进!

将动画的位置设置为指定的时间点。该时间应介于 0 和动画的总持续时间(包括任何重复)之间。如果动画还没有开始,那么设置到这个时间后就不再前进;它只会将时间设置为该值,并根据该时间执行任何适当的操作。如果动画已经在运行,则 setCurrentPlayTime() 会将当前播放时间设置为此值并从该点继续播放。http://developer.android.com/reference/android/animation/ValueAnimator.html#setCurrentPlayTime(long)

    private void stopAnimation(){
        mCurrentPlayTime = mRotateAntiClockwiseAnimator.getCurrentPlayTime();
        mRotateAntiClockwiseAnimator.cancel();
    }

    private void startAnimation() {
            mRotateAntiClockwiseAnimator.start();
            mRotateAntiClockwiseAnimator.setCurrentPlayTime(mCurrentPlayTime);
    }
Run Code Online (Sandbox Code Playgroud)