首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >让Android AnimatorSet停止动画

让Android AnimatorSet停止动画
EN

Stack Overflow用户
提问于 2014-08-12 07:33:55
回答 2查看 9.7K关注 0票数 6

我有以下AnimatorSet方法:

代码语言:javascript
复制
private AnimatorSet dialCenterThrob() {
    int bpm = workoutStream.getHeartRate();
    dialCenterImageView.clearAnimation();
    AnimatorSet finalSet = new AnimatorSet();

    ObjectAnimator pulseX = ObjectAnimator.ofFloat(dialCenterImageView, View.SCALE_X, 0.98f, 1.06f);
    ObjectAnimator pulseY = ObjectAnimator.ofFloat(dialCenterImageView, View.SCALE_Y, 0.98f, 1.06f);

    pulseX.setRepeatMode(ObjectAnimator.REVERSE);
    pulseX.setRepeatCount(ObjectAnimator.INFINITE);
    pulseY.setRepeatMode(ObjectAnimator.REVERSE);
    pulseY.setRepeatCount(ObjectAnimator.INFINITE);
    pulseX.setDuration(bpm);
    pulseY.setDuration(bpm);
    pulseX.setInterpolator(new AccelerateInterpolator());
    pulseY.setInterpolator(new AccelerateInterpolator());

    finalSet.playTogether(pulseX, pulseY);

    return finalSet;
}

这是在名为throbber的var上设置的,偶尔会通过以下方法进行更新:

代码语言:javascript
复制
private void updateThrobbing() {
    if (hasThrob()) {
        throbber = dialCenterThrob();
        throbber.start();
    } else {
        if (throbber != null && throbber.isRunning()) {
            stopThrobbing();
        }
    }
}

但是我不能让它停止动画,下面是目前尝试这样做的方法:

代码语言:javascript
复制
public void stopThrobbing() {
    List<Animator> throbbers = throbber.getChildAnimations();
    for(Animator animator : throbbers) {
        //accomplishes nothing
        ((ObjectAnimator)animator).setRepeatCount(0);
        ((ObjectAnimator)animator).setRepeatMode(0);
    }

    throbber.pause(); //nothing
    throbber.cancel(); //and again, nothing
    throbber.end();//shocking, I know, but really, nothing
    throbber = null;//you'd think this would definitely do it, but no
    //desparate attempt, in vein, of course
    dialCenterImageView.clearAnimation();
}

我不能让它停止动画。更新:我只是尝试将本地引用存储到各个对象动画师,然后在每个对象上调用setRepeatCount、mode、pause、end、cancel,但仍然一无所获。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-14 03:19:04

dialCenterImageView.clearAnimation();

这将使不影响由ObjectAnimator创建的动画上的。您只能清除通过startAnimation()在视图上启动的动画

代码语言:javascript
复制
findViewById(R.id.yourViewId).startAnimation(yourAnimation);
findViewById(R.id.yourViewId).clearAnimation();

任何对象动画师,或repeatCount设置为无限的动画集都不会停止,无论您做什么,除非离开视图。

这是真的!今天,我以一种艰难的方式学到了这个。因此,我将补充我的两点意见。您需要存储ObjectAnimator实例的引用,并在以后对其调用cancel()

当旋转android手机屏幕后,动画仍在继续时,我遇到了这种情况,在这种情况下,你的活动基本上是用新的布局重新创建的。

这就是我所做的

代码语言:javascript
复制
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    ObjectAnimator buttonOneObjectAnimator = myOnClickListener.getButtonOneColorAnim();
    if(buttonOneObjectAnimator != null)
        buttonOneObjectAnimator.cancel();
}

onPause()中也可以很好地做到这一点。

另一个需要注意的重要问题。如果在ObjectAnimator实例上调用start() n次,则必须调用cancel() n次才能完全停止动画。

另外,如果您添加了AnimatorSet侦听器,则在调用cancel之前,请确保已删除这些侦听器。

代码语言:javascript
复制
yourAnimatorSet.removeAllListeners();
yourAnimatorSet.end();
yourAnimatorSet.cancel();
票数 29
EN

Stack Overflow用户

发布于 2018-03-04 02:32:32

但是,我所做的,并不是因为谷歌习惯于半途而废,而是在它的标记中传递了视图的ObjectAnimator实例,为了能够阻止它,需要知道这个实例。

示例开始/取消视图上的无限旋转...

代码语言:javascript
复制
private void rotateStart(View view) {
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", 0f, 360f);
    // set animator instance as view's tag
    view.setTag(animator); 
    animator.setDuration(800);
    animator.setRepeatMode(ObjectAnimator.RESTART);
    animator.setRepeatCount(ObjectAnimator.INFINITE);
    animator.start();
}

private void rotateCancel(View view) {
    // get view's tag, which is its animator instance
    ObjectAnimator animator = (ObjectAnimator) view.getTag();
    if (animator != null) {
        animator.cancel();
    }
}

我假设这将完成这项工作,无论它是单个动画还是一组动画。

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

https://stackoverflow.com/questions/25254046

复制
相关文章

相似问题

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