我知道api级别19在objectanimators上支持pause()和resume()。但是在我的api级别14的项目中,我有一个objectanimator,它应用于图像视图来旋转它。我想在触摸时暂停objectanimator提供的动画,并从图像视图所在的位置(在触摸之前)恢复它。
因此,我试图保存当前播放时间,并取消stopAnimation()函数上的对象动画师。

private void stopAnimation(){
        currentTime = mGlobeAnimator.getCurrentPlayTime();
        mGlobeAnimator.cancel();
    }

在startanimation()函数中,我重新创建动画制作程序,将其目标设置为图像视图,设置保存的播放时间并启动它。
private void startAnimation(Context context, View view, float startAngle) {
        ObjectAnimator globeAnimatorClone = (ObjectAnimator)AnimatorInflater.loadAnimator(context, R.animator.rotate_globe);
        globeAnimatorClone.setTarget(mImageView);
        globeAnimatorClone.setCurrentPlayTime(currentTime);
        globeAnimatorClone.start();
}

这不起作用。请问有谁能帮助暂停和恢复动画的任何指针,由动画提供的API级在19之前?

最佳答案

我想我是通过启动动画师然后设置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);
}

07-28 04:06