首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何停止动画(来自ObjectAnimator)

如何停止动画(来自ObjectAnimator)
EN

Stack Overflow用户
提问于 2015-12-10 02:09:45
回答 4查看 5.5K关注 0票数 3

下面是属于我的活动的代码,其中我有一个按钮(someButton),当单击该按钮时,将在同一活动的另一个视图上启动动画,更具体地说,是进度栏视图:

代码语言:javascript
复制
// [...]
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上的情况一样):

代码语言:javascript
复制
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)
[...]

抛出这个异常的代码是:

代码语言:javascript
复制
// [...]

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!
    }
});
// [...]

我到底做错了什么??怎样才能停止这个动画?

EN

回答 4

Stack Overflow用户

发布于 2018-12-15 16:37:46

您可以这样使用:

代码语言:javascript
复制
valueAnimator?.removeAllUpdateListeners()
valueAnimator?.removeAllListeners()
valueAnimator?.end()
valueAnimator?.cancel()
valueAnimator = null
票数 2
EN

Stack Overflow用户

发布于 2015-12-10 02:31:35

首先,你可以在this link上找到更好的例子。

我还建议您不要停止动画(除非您使用扩展视图、postDelay处理程序和线程休眠来正确地实现它。如果某些逻辑已经完成,只需删除视图/打开另一个屏幕,特别是如果它是progressView,它不需要显示,但不旋转,只需删除它的可见性。

祝好运

票数 0
EN

Stack Overflow用户

发布于 2017-07-13 21:09:44

尝试: animation.setCurrentPlayTime(0);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34185966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档