Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueAnimator only repeats once

I am trying to get a ValueAnimator to repeat once it has ended. I am using it with a SeekBar in a ListView. For some reason, the ValueAnimator will finish, trigger the onAnimationEnd() go again, but then when it reaches the end the onAnimationEnd() is never called a second time.

    @Override
    public View getContentView(int position, View convertView, ViewGroup parent) {
        ...
        setupTimerBar(t);
        ...
    }


    private AnimatorListenerAdapter generateNewAnimatorListenerAdapter(final TylersContainer t) {
        return new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                setupTimerBar(t);
            }
        };
    }

    private void setupTimerBar(TylersContainer t)
    {       
        View view = t.getView();
        BusTime arrivalTime = t.getBusTime();
        int minutes = BusTime.differenceInMiuntes(arrivalTime, BusTime.now());
        long milliseconds = minutes * 60 * 1000;

        final TimerBar seekBar = (TimerBar) view.findViewById(R.id.SeekBar);

        int progress = Utility.setProgress(arrivalTime, seekBar.getMax());  
        long duration = Utility.setAnimationDuration(progress);

        seekBar.setProgress(progress);
        seekBar.setAnimationDuration(duration);
        seekBar.setAnimationStartDelay(milliseconds);
        seekBar.setAnimatorListenerAdapter(generateNewAnimatorListenerAdapter(t));
    }

The seekBar object is actually an custom object which contains a SeekBar and a ValueAnimator, here are the relevant bits:

    //Constructor
    public TimerBar(Context context) {
        super(context);

        startTime = Calendar.getInstance();
        valueAnimator = ValueAnimator.ofInt(0, getMax());

        //Override the update to set this object progress to the animation's value
        valueAnimator.addUpdateListener(new AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {    
                int animProgress = (Integer) animation.getAnimatedValue();
                setProgress(animProgress);

            }
         });
    }

    //Specify the start time by passing a long, representing the delay in milliseconds
    public void setAnimationStartDelay(long milliseconds){

        //Set the delay (if need be) and start the counter
        if(milliseconds > 0)
            valueAnimator.setStartDelay(milliseconds);

        valueAnimator.setIntValues(this.getProgress(), this.getMax());

        valueAnimator.start();
    }


    //Set the duration of the animation
    public void setAnimationDuration(long duration){
        valueAnimator.setDuration(duration);
    }


    public void setAnimatorListenerAdapter(AnimatorListenerAdapter ala){
        valueAnimator.addListener(ala);
    }

I can't figure out why it isn't repeating more than twice.

I've tried using the Repeat attribute and setting that to INIFINITI but that didn't help either.


Edit: To be clear, what I am trying to get is an animation that repeats itself indefinitely, each time with a different duration.

like image 943
Tyler Avatar asked Dec 18 '13 02:12

Tyler


2 Answers

I have made the error of setting the RepeatMode to infinite which did not work, this must be set as the RepeatCount:

valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
like image 62
weston Avatar answered Nov 08 '22 06:11

weston


In case you are using Animator, and then bind it with Animator.AnimatorListener

The function AnimatorListener.onAnimationEnd() is used in case your animation repeats for finite times, and is only called once

In case your animation is repeating, for number of times, you should be using the function AnimatorListener.onAnimationRepeat() instead, which will apply everytime your animation repeats after the end of each repeatance

From what I understand, what you need is onAnimationRepeat(), so if you just move the code that you want to be executed after each repeat from onAnimationEnd() to onAnimationRepeat(), this should fix it

Reference: http://developer.android.com/reference/android/animation/Animator.AnimatorListener.html

like image 1
Khaled.K Avatar answered Nov 08 '22 06:11

Khaled.K