下面是属于我的活动的代码,其中我有一个按钮(someButton),当单击该按钮时,将在同一活动的另一个视图上启动动画,更具体地说,是进度栏视图:
// [...]
someButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ObjectAnimator animation = ObjectAnimator.ofInt(progressView, "progress", 0, 2000);
animation.setDuration(2000);
animation.setInterpolator(new DecelerateInterpolator());
animation.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
// some code to execute when the animation ends
}
});
animation.start();
}
});
// [...]现在,在某些时候,我可能需要停止进度条视图的动画(例如,当用户点击停止按钮时)。我调用progressView.clearAnimation()来停止动画,但是没有成功。我还注意到progressView.getAnimation()返回null...当我将ObjectAnimator animation变量设为final并将其移到someButton.setOnClickListener之外,以便以后可以访问它来停止动画时,当我点击someButton时,我得到了以下异常(就像发生在here上的情况一样):
java.lang.NullPointerException
at android.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:505)
at android.animation.ObjectAnimator.initAnimation(ObjectAnimator.java:487)
at android.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:517)
at android.animation.ValueAnimator.start(ValueAnimator.java:936)
at android.animation.ValueAnimator.start(ValueAnimator.java:946)
at android.animation.ObjectAnimator.start(ObjectAnimator.java:465)
at android.animation.AnimatorSet$1.onAnimationEnd(AnimatorSet.java:579)
at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1056)
at android.animation.ValueAnimator.access$400(ValueAnimator.java:50)
[...]抛出这个异常的代码是:
// [...]
final ObjectAnimator animation = ObjectAnimator.ofInt(progressView, "progress", 0, 2000);
animation.setDuration(2000);
animation.setInterpolator(new DecelerateInterpolator());
animation.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
// some code to execute when the animation ends
}
});
someButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
animation.start();// NPE occurs here!
}
});
// [...]我到底做错了什么??怎样才能停止这个动画?
发布于 2018-12-15 16:37:46
您可以这样使用:
valueAnimator?.removeAllUpdateListeners()
valueAnimator?.removeAllListeners()
valueAnimator?.end()
valueAnimator?.cancel()
valueAnimator = null发布于 2015-12-10 02:31:35
首先,你可以在this link上找到更好的例子。
我还建议您不要停止动画(除非您使用扩展视图、postDelay处理程序和线程休眠来正确地实现它。如果某些逻辑已经完成,只需删除视图/打开另一个屏幕,特别是如果它是progressView,它不需要显示,但不旋转,只需删除它的可见性。
祝好运
发布于 2017-07-13 21:09:44
尝试: animation.setCurrentPlayTime(0);
https://stackoverflow.com/questions/34185966
复制相似问题