Home > Back-end >  Android animation rotate infinite, without pausing
Android animation rotate infinite, without pausing

Time:12-20

I can rotate my image infinitely. But my problem is that the image pauses shortly when it reaches 360º and then starts rotating again. It happens the same even when I applied "linear_interpolator". What I want to do is that the image does not pause at all when it starts the next round. So it has to rotate infinitely with same speed at any degree.

Here is my - code. Thanks

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
    android:interpolator="@android:anim/linear_interpolator"
    android:duration="1400"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:repeatMode="restart"
    android:repeatCount="infinite" />
</set>

How I call it on my code

    rotate= AnimationUtils.loadAnimation(context, R.anim.loop_rotate)
    binding.imgSecondLayout.startAnimation(rotate)

Thanks for help! :)

CodePudding user response:

This is due to the small delay after the animation completes its duration (1400 ms in ur case). you can remove this delay for smooth animation.

Remove repeatMode attribute and instead add this line :

android:startOffset="0"  //Delay in milliseconds before the animation runs

The animation will be smooth without any delays

CodePudding user response:

Just add animation.setRepeatCount(Animation.INFINITE) to your java class where animation is called.

My final code is given here:

Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.loop_rotate);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatCount(Animation.INFINITE);
animation.setDuration(1400);
youractivity.startAnimation(animation);
  • Related