ViewAnimator
ViewAnimator copied to clipboard
Problem with looping to define an animation
Hello.
I would like to make a loop and modify the ViewAnimator on the go.
So, theorically making this:
ViewAnimator viewAnimator = ViewAnimator.animate(heart_progress).translationY(0, heart_progress.getHeight() / maxSeconds).descelerate().duration(1000);
for (int i = 1; i < maxSeconds; i++) {
final int finalI = i;
viewAnimator.thenAnimate(heart_progress).translationY((i % maxSeconds) * heart_progress.getHeight() / maxSeconds, ((i + 1) % maxSeconds) * heart_progress.getHeight() / maxSeconds).descelerate().onStart(new AnimationListener.Start() {
@Override
public void onStart() {
System.out.println("animation #"+ finalI);
}
}).duration(1000);
}
viewAnimator.start();
would be the same than doing this:
ViewAnimator viewAnimator = ViewAnimator.animate(heart_progress).translationY(0, heart_progress.getHeight() / maxSeconds).descelerate().duration(1000).
thenAnimate(heart_progress).translationY((1 % maxSeconds) * heart_progress.getHeight() / maxSeconds, ((1 + 1) % maxSeconds) * heart_progress.getHeight() / maxSeconds).descelerate().onStart(new AnimationListener.Start() {
@Override
public void onStart() {
System.out.println("animation #"+ 1);
}
}).duration(900).
thenAnimate(heart_progress).translationY((2 % maxSeconds) * heart_progress.getHeight() / maxSeconds, ((2 + 1) % maxSeconds) * heart_progress.getHeight() / maxSeconds).descelerate().onStart(new AnimationListener.Start() {
@Override
public void onStart() {
System.out.println("animation #"+ 2);
}
}).duration(900).
thenAnimate(heart_progress).translationY((3 % maxSeconds) * heart_progress.getHeight() / maxSeconds, ((3 + 1) % maxSeconds) * heart_progress.getHeight() / maxSeconds).descelerate().onStart(new AnimationListener.Start() {
@Override
public void onStart() {
System.out.println("animation #"+ 3);
}
}).duration(900).
thenAnimate(heart_progress).translationY((4 % maxSeconds) * heart_progress.getHeight() / maxSeconds, ((4 + 1) % maxSeconds) * heart_progress.getHeight() / maxSeconds).descelerate().onStart(new AnimationListener.Start() {
@Override
public void onStart() {
System.out.println("animation #"+ 4);
}
}).duration(900).
thenAnimate(heart_progress).translationY((5 % maxSeconds) * heart_progress.getHeight() / maxSeconds, ((5 + 1) % maxSeconds) * heart_progress.getHeight() / maxSeconds).descelerate().onStart(new AnimationListener.Start() {
@Override
public void onStart() {
System.out.println("animation #"+ 5);
}
}).duration(900).
thenAnimate(heart_progress).translationY((6 % maxSeconds) * heart_progress.getHeight() / maxSeconds, ((6 + 1) % maxSeconds) * heart_progress.getHeight() / maxSeconds).descelerate().onStart(new AnimationListener.Start() {
@Override
public void onStart() {
System.out.println("animation #"+ 6);
}
}).duration(900).start();
but the result is pretty different:
CORRECT

(please allow some time to load the GIF and an offset to start the video :D)
WRONG

(please allow some time to load the GIF and an offset to start the video :D)
What's wrong with the for loop?
Thanks.
I seem also encountered the same problem.