How can I repeat a property animation?
How can I repeat a property animation?
I'm doing an animation of bubbles on the screen, but the bubbles stop after finishing the animation time. How do I repeat the animation or make it infinite?
bub.animate();
bub.animate().x(x2).y(y2);
bub.animate().setDuration(animationTime);
bub.animate().setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
animators.add(animation);
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
});
You can also use
ValueAnimator which supports repeats, developer.android.com/reference/android/animation/…– ZhiXingZhe - WangYuQi
Apr 9 at 11:39
ValueAnimator
4 Answers
4
Since ViewPropertyAnimator is only good for simple animations, use more advanced ObjectAnimator class - basically method setRepeatCount and additionally setRepeatMode.
ViewPropertyAnimator
ObjectAnimator
it does not work in PropertyAnimator
– user3901032
Aug 17 '14 at 0:18
Yes,
ViewPropertyAnimator is used for simple animations, for more complex usage, try ObjectAnimator. Sorry for unclear answer.– asylume
Aug 17 '14 at 0:52
ViewPropertyAnimator
ObjectAnimator
This is actually possible. Here's an example of rotating a view:
final ViewPropertyAnimator animator = view.animate().rotation(360).setInterpolator(new LinearInterpolator()).setDuration(1000);
animator.setListener(new android.animation.Animator.AnimatorListener() {
...
@Override
public void onAnimationEnd(final android.animation.Animator animation) {
animation.setListener(null);
view.setRotation(0);
view.animate().rotation(360).setInterpolator(new LinearInterpolator()).setDuration(1000).setListener(this).start();
}
});
You can also use "withEndAction" instead of a listener.
You can use CycleInterpolator. For example, like this:
CycleInterpolator
int durationMs = 60000;
int cycleDurationMs = 1000;
view.setAlpha(0f);
view.animate().alpha(1f)
.setInterpolator(new CycleInterpolator(durationMs / cycleDurationMs))
.setDuration(durationMs)
.start();
How would you make it infinitely animate ?
– android developer
Mar 30 '17 at 12:48
final ViewPropertyAnimator animator = view.animate().rotation(360).setInterpolator(new LinearInterpolator()).setDuration(1000); //Animator object
animator.setListener(new android.animation.Animator.AnimatorListener() {
...
@Override
public void onAnimationEnd(final android.animation.Animator animation) {
animation.setListener(this); //It listens for animation's ending and we are passing this to start onAniationEnd method when animation ends, So it works in loop
view.setRotation(0);
view.animate().rotation(360).setInterpolator(new LinearInterpolator()).setDuration(1000).setListener(this).start();
}
});
While this code may answer the question, providing information on how and why it solves the problem improves its long-term value
– L_J
Jun 30 at 17:16
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
possible duplicate of How to generate looping animation with ViewPropertyAnimator?
– kaka
Aug 17 '14 at 0:12