6

我有以下 AnimatorSet 方法:

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 上设置的,并且偶尔会通过以下方法更新:

private void updateThrobbing() {
    if (hasThrob()) {
        throbber = dialCenterThrob();
        throbber.start();
    } else {
        if (throbber != null && throbber.isRunning()) {
            stopThrobbing();
        }
    }
}

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

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、模式、暂停、结束、取消,但仍然没有。

4

2 回答 2

29

dialCenterImageView.clearAnimation();

不会影响ObjectAnimator. 您只能通过以下方式清除已在视图上启动的动画startAnimation()

findViewById(R.id.yourViewId).startAnimation(yourAnimation);
findViewById(R.id.yourViewId).clearAnimation();

任何对象 animator 或 repeatCount 设置为 Infinite 的 animatorset 都不会停止,无论您做什么,都不会离开视图。

诚然!今天很难学到这一点。所以会加我的两分钱。您需要存储ObjectAnimator实例的引用并稍后调用cancel()它。

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

所以这就是我所做的

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    ObjectAnimator buttonOneObjectAnimator = myOnClickListener.getButtonOneColorAnim();
    if(buttonOneObjectAnimator != null)
        buttonOneObjectAnimator.cancel();
}

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

另一个需要注意的重要点。如果您调用start()实例ObjectAnimatorn,则必须调用cancel() n 次才能使动画完全停止。

此外,如果您AnimatorSet添加了侦听器,请确保在调用取消之前已删除侦听器。

yourAnimatorSet.removeAllListeners();
yourAnimatorSet.end();
yourAnimatorSet.cancel();
于 2015-05-13T19:19:04.203 回答
3

这里的响应迟了,但是,我所做的,不感谢谷歌的半生不熟的习惯,是在它的标签中传递视图的 ObjectAnimator 实例,需要知道它才能阻止它。

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

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();
    }
}

我认为无论是单个动画还是一组动画,这都能胜任。

于 2018-03-03T18:32:32.230 回答