【问题标题】:How to start Android animation from the values where it was stopped?如何从停止的值开始 Android 动画?
【发布时间】:2014-08-06 18:06:48
【问题描述】:

我有一个应用旋转动画的图像视图。动画效果很好。在着陆时,我尝试使用 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"/>

我正在尝试使用以下代码停止动画

private void stopAnimation(){
        mRotateAntiClockwiseAnimation.cancel();
    mRotateAntiClockwiseAnimation.reset();
    imageView.clearAnimation();
    mRotateAntiClockwiseAnimator.end();
    mRotateAntiClockwiseAnimator.cancel();
    stopAnimationForImageButton();
    }

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

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

如您所见,即使使用 cancel() 或 reset() 也无助于在动画被触碰的地方停止动画。任何指针都会有所帮助

【问题讨论】:

  • 贴出你使用动画xml的代码
  • @Rod_Algonquin:更新了我用动画设置视图的代码
  • 也发布 stopAnimationForImageButton 方法
  • stopAnimationForImageButton() 只是停止我在布局上的图像按钮的对象动画师,它也应该与图像视图一起移动。 private void stopAnimationForImageButton() { for(ObjectAnimator objectAnimator : mObjectAnimatorList) { objectAnimator.cancel(); objectAnimator.end(); } mObjectAnimatorList.clear(); }
  • @Rod_Algonquin:我已经发布了相关代码。你能帮忙指点一下吗?或者你认为这里有什么问题?

标签: android animation


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在 API 级别 19 中添加了暂停功能。Here 您可以阅读如何实现它。此方法使用ObjectAnimator,它并不比您使用的通常的Animation 类复杂多少。但是,还有另一个有用的替代方案 trick
    致以最诚挚的问候。

    【讨论】:

    • 我现在改用 ObjectAnimator,因为它很简单。但我处于 API 级别 14,因此无法使用 pause() 或 resume()。在 API 级别 14 的 ObjectAnimator 中是否可以使用 pause() 和 resume() 替代方法?请帮忙
    • 这是个有趣的问题。事实上,答案是否定的。但是您可以尝试使用 cancel() 方法暂停,当您想恢复时,只需使用 clone() 方法重新创建/克隆 ObjectAnimator并将动画的当前位置及时手动设置到这个ObjectAnimator实例中。为此,请在暂停前使用 ObjectAnimator.getCurrentPlayTime()。我还没有尝试过这种方法,但它似乎有效。
    • 你的意思是说我做以下? private long currentTime; private ObjectAnimator globeAnimator; private void stopAnimation() { currentTime = globeAnimator.getCurrentPlayTime(); globeAnimator.cancel(); } private void startAnimation() { ObjectAnimator globeAnimatorClone = globeAnimator.clone(); globeAnimatorClone.setTarget(globeImageView); globeAnimatorClone.setCurrentPlayTime(currentTime); globeAnimatorClone.start(); }
    • 不,它似乎不起作用。有什么问题吗?
    • cancel() 之前尝试 clone() ObjectAnimator。或者最好不要克隆 ObjectAnimator 并创建新的 ObjectAnimator 并将其初始化为您的第一个 ObjectAnimator 并使用 setCurrentPlayTime(currentTime)
    猜你喜欢
    • 2022-11-25
    • 1970-01-01
    • 2013-05-19
    • 2011-11-30
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    相关资源
    最近更新 更多