valueanimator.start 在 API < 19 的 valueanimator.cancel 之后重新开始动画

valueanimator.start is restaring the animation after valueanimator.cancel on API < 19

我知道这是在我尝试所有解决方案之前被问到的,但我无法到达修复它, 我使用 android 库中的翻译让对象从屏幕顶部掉落到屏幕底部。

我想暂停并恢复动画:

根据对其中一篇堆栈溢出帖子的建议修复了暂停代码:

private void pauseAnimation() {
        p = true;
        Pausearray = new MyClass[mAllImageViews.size()];
        int count = 0;
        for (ArrowView v : mAllImageViews) {
            ValueAnimator va = (ValueAnimator) v.getTag();
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                va.pause();
            } else {
                if (va != null) {
                    MyClass temp = new MyClass();
                    temp.tag = v.getTag().toString();
                    temp.playtime = va.getCurrentPlayTime();
                    Pausearray[count] = temp;
                    va.cancel();
                }
            }
            count ++;
        }
    }

暂停正在工作动画正在停止移动并保持在原位。

堆栈溢出帖子之一建议的恢复代码:

   private void resumeAnimation() {
        p = false;
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            for (ArrowView v : mAllImageViews) {
                try {
                    ValueAnimator va = (ValueAnimator) v.getTag();
                    va.resume();
                } catch (Exception ex) {

                }
            }
        } else {
            if(Pausearray!=null) {
                for (ArrowView v : mAllImageViews) {
                    ValueAnimator va = (ValueAnimator) v.getTag();
                    for (int i = 0; i < Pausearray.length; i++) {
                        if(v.getTag().toString().equalsIgnoreCase(Pausearray[i].tag)){
                            va.start();
                            va.setCurrentPlayTime(Pausearray[i].playtime);
                        }
                    }
                }
            }
        }
    }

简历的问题是它再次从顶部开始我希望它继续它在屏幕上的位置。谢谢!

更新:

我试图从屏幕上获取箭头视图位置并在恢复时设置它,我仍然遇到同样的问题,视图正在重新启动所以编辑后的代码是:

  private void resumeAnimation() {
        p = false;
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            for (ArrowView v : mAllImageViews) {
                try {
                    ValueAnimator va = (ValueAnimator) v.getTag();
                    va.resume();
                } catch (Exception ex) {

                }
            }
        } else {
            if(Pausearray!=null) {
                for (ArrowView v : mAllImageViews) {
                    ValueAnimator va = (ValueAnimator) v.getTag();
                    for (int i = 0; i < Pausearray.length; i++) {
                        if(v.getTag().toString().equalsIgnoreCase(Pausearray[i].tag)){
                            Log.w("PAUSETEST","Setting Parameters "+Pausearray[i].playtime);
                            va.start();
                            va.setCurrentPlayTime(Pausearray[i].playtime);
                            v.setY(Pausearray[i].translationy); // i took the location via v.gettranslationY on pause
                        }
                    }
                }
            }
        }
    }

更新: 我在搜索时听说 valueanimator 没有重新启动,所以我将 valueanimator 切换为对象动画器动画器代码:

 public void startAnimation(final ArrowView aniView) {
    aniView.setPivotX(aniView.getWidth());
    aniView.setPivotY(aniView.getHeight());

    long delay = new Random().nextInt(Constants.MAX_DELAY);// generate a random delay time before the animation starts to go down

    ObjectAnimator animator = ObjectAnimator.ofFloat(aniView,"translationY", mDisplaySize.bottom - (buttonsize) - (90 * mScale));
    if (aniView.getColor() == 0 && aniView.draw == false) {
        Constants.ANIM_DURATION = Constants.ANIM_DURATION_NORMAL;
    } else if (aniView.getColor() != 0 && aniView.draw == false) {
        Constants.ANIM_DURATION = Constants.ANIM_DURATION_COLOR;
    } else if (aniView.getColor() == 0 && aniView.draw == true) {
        Constants.ANIM_DURATION = Constants.ANIM_DURATION_COLOR;
    } else if (aniView.getColor() != 0 && aniView.draw == true) {
        Constants.ANIM_DURATION = Constants.ANIM_DURATION_COLORANDCALC;
    }
    animator.setDuration(Constants.ANIM_DURATION);// speed of the falldown
    animator.setInterpolator(new AccelerateInterpolator());
    animator.setStartDelay(delay);// start the animation after the delay.
//        animator.addUpdateListener(new AnimatorUpdateListener() {
//
//            //int angle = 50 + (int) (Math.random() * 101);
//            //int movex = new Random().nextInt(mDisplaySize.right);
//
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                float value = ((Float) (animation.getAnimatedValue())).floatValue();
//                //aniView.setRotation(angle*value);
//                //aniView.setTranslationX((movex-40)*value);
//                aniView.setTranslationY((mDisplaySize.bottom - (buttonsize)) * value - (90 * mScale));// set the translation to fall down and stop on the top of buttons
//            }
//        });
    aniView.setTag(animator);
    animator.start();
}

我更改了 pause/resume 的代码以将其转换为对象动画师,但我仍然面临同样的问题...开始时正在重新启动动画!请有人帮助!谢谢!

在尝试使用 valueanimator class 之后,我想出了一个让动画停止并恢复的技巧,这不是一种真实的方式,但它解决了问题,我从 value animator class 扩展而来并用 myvalueanimator class 替换了我的 main class 中的普通动画师,这里是值动画师 class:

import android.animation.ValueAnimator;
import android.util.Log;

import java.util.Timer;

/**
 * Created by win7 on 6/21/2017.
 */

public class MyValueAnimator extends ValueAnimator {
    private float animatedValue = 0;
    private long currenttime;
    private long totaltime;
    private boolean onetime = true;
    private volatile boolean mPaused = false;
    private Timer t;

public void pause() {
    animatedValue = (float) getAnimatedValue();
    mPaused = true;
}

//    @Override
//    public void setValues(PropertyValuesHolder... values) {
//        if (mPaused)
//            return;
//        super.setValues(values);
@Override
public Object getAnimatedValue() {
    if (mPaused) {
        if(onetime){
            currenttime = getCurrentPlayTime();
         //   totaltime = getStartDelay()+ (getDuration() * (getRepeatCount() + 1));
            totaltime = getDuration();
            setDuration(Long.parseLong("9999999999999999"));
            Log.w("PauseDur",Long.parseLong("9999999999999999")+"");
            onetime =false;
        }
        return animatedValue;
    }
    return super.getAnimatedValue();
}

public void resume() {
    mPaused = false;
    onetime=true;
    setCurrentPlayTime(currenttime);
    setDuration(totaltime);
}

public MyValueAnimator(float from, float to) {
    setFloatValues(from, to);
}

}