10

I want to build an animation of TextViews, which repeats itself just after completion.

For each View I want to animate, I use the following piece of code

final float oldX = v.getX();
final float newX = v.getX() - (float)totalWidth;
final AnimatorListenerAdapter listener = new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        v.setX(oldX);
        animFinished = true;

        //This line won't compile
        //v.animate().setDuration(animDuration).setInterpolator(newsInterpolator)
        //    .setListener(listener).x(newX);
    }
};

v.animate().setDuration(animDuration).setInterpolator(newsInterpolator)
    .setListener(listener).x(newX); 

I tried to place the last piece of code into the onAnimationEnd, but Java will not compile since it considers the object listener as not initialized. Moreover, I don't think that this "recursive" animation invocation is a good solution, it was the first thing which came to my mind. I am suspicious that there is a simple and sound way to implement looping property animation, but I failed to locate it, so I turned here for help.

Thanks in advance

jww
  • 97,681
  • 90
  • 411
  • 885
Ufuk Can Bicici
  • 3,589
  • 4
  • 28
  • 57
  • You can use `CycleInterpolator`. See my answer [here](http://stackoverflow.com/a/40385244/2093236). – Dmide Nov 02 '16 at 16:42
  • I think "this" would be what to use instead of listener, but yeah it'd probably just hold onto the memory and leak if that's what happened. Anyway I realize this is nearly 4 years old by now. – Stephen J Mar 28 '17 at 19:36

2 Answers2

8

Well, I am going to answer myself again.

TranslateAnimation class has methods about repeating the animation, so I used it instead of ViewPropertyAnimator.

The following code seems to work:

            long duration = 1000* ((long)totalWidth / newsScrollSpeed);
            System.out.println("totalWidth="+totalWidth);
            TranslateAnimation  anim = new TranslateAnimation(0,-totalWidth,0,0);
            anim.setInterpolator(linearInterpolator);
            anim.setDuration(duration);
            anim.setRepeatCount(TranslateAnimation.INFINITE);
            anim.setRepeatMode(TranslateAnimation.RESTART);

            for(i=0;i<this.getChildCount();i++)
            {
                View v = this.getChildAt(i);

                if(v.getId() == R.id.yuruyen_yazi)
                {
                    continue;
                }

                v.startAnimation(anim);
            }
Ufuk Can Bicici
  • 3,589
  • 4
  • 28
  • 57
3

Not elegant way, but it works:

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        // update newX
        v.animate().setDuration(animDuration).setInterpolator(newsInterpolator).x(newX).withEndAction(this).start(); 
    }
};

v.animate().setDuration(animDuration).setInterpolator(newsInterpolator).x(newX).withEndAction(runnable).start(); 
Vitaly Zinchenko
  • 4,871
  • 5
  • 36
  • 52