Create Animations for Android AnimatorSet

I have the following AnimatorSet method:

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

      

This is set in a var called throbber and is sometimes updated with this method:

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

      

But I can't get it to stop animating, and here is the method currently trying to do it:

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

      

I can't get it to stop animating. Update: I just tried to keep the local ref for separate object animators and then called setRepeatCount, mode, pause, end, cancel on each one and still nothing.

+3


source to share


2 answers


dialCenterImageView.clearAnimation ();

When the animation is created ObjectAnimator

, this will not affect . You can only clear the animation you started in the viewstartAnimation()

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

      

any animator object, or animatorset with repeatCount set to Infinite, will not pause no matter what you do without leaving the view.

It is true that! I learned this the hard way today. So add my two cents. You need to keep the link of the instance ObjectAnimator

and then call cancel()

on it.

I ran into this where the animation was still continuing after the screen rotation of your android phone, in which case your activity was essentially created with a new layout.



So this is what I did

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

      

You can do this very well in onPause()

.

Another important point. If you call start()

on your ObjectAnimator

instance n times , you will need to call cancel()

n times to completely stop the animation.

Also, if you added listeners AnimatorSet

, make sure you remove the listeners before calling cancellation.

yourAnimatorSet.removeAllListeners();
yourAnimatorSet.end();
yourAnimatorSet.cancel();

      

+19


source


Late posted here, but what I did, not thanks to google's habit of leaving things half-baked, was to pass an ObjectAnimator View instance to its tag, which must be known to be able to stop it.

An example of starting / canceling an infinite rotation in a view ...



private void rotateStart(View view) {
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", 0f, 360f);
    // set animator instance as view tag
    view.setTag(animator); 
    animator.setDuration(800);
    animator.setRepeatMode(ObjectAnimator.RESTART);
    animator.setRepeatCount(ObjectAnimator.INFINITE);
    animator.start();
}

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

      

I would guess it would make the job, whether it was a standalone animation or a set.

0


source







All Articles