android - 如何从停止的值开始 Android 动画?

标签 android animation

我有一个应用旋转动画的 ImageView 。动画效果很好。着陆时,我尝试使用 cancel() 停止旋转动画,使用 reset() 重置动画并使用 clearAnimation() 清除 View 上的动画。但是动画又回到了原来的位置。如何使用触地事件发生时的值停止动画并从停止的地方重新开始?

我的动画在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();
    }

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

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

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

最佳答案

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

Sets the position of the animation to the specified point in time. This time should be between 0 and the total duration of the animation, including any repetition. If the animation has not yet been started, then it will not advance forward after it is set to this time; it will simply set the time to this value and perform any appropriate actions based on that time. If the animation is already running, then setCurrentPlayTime() will set the current playing time to this value and continue playing from that point. 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);
    }

关于android - 如何从停止的值开始 Android 动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25167153/

相关文章:

java - 如何将分钟添加到 EventdateTime 并转换为 DateTime

android - 两个 ViewPagers 同步滚动

ios - 交互式过渡在取消时奇怪地闪烁

ios - UIView animateWithDuration 在 iOS 8 中表现不同

android - 是否可以在对话框 fragment 内显示 map View ?

java - android studio,哪个导入语句使用外部库?

javascript - D3 转换 : duration doesn't work for line path

c++ - WIN32 上 "iPhone"风格动画的最佳 2D 动画库/技术?

java - 我的 Android 应用程序崩溃并给出非法访问异常,我尝试实现 TCP 服务来与我的服务器通信

ios - 带有自动反转的 UIView 动画