I’ve defined RotateAnimation to rotate a ImageView. So, I want to stop the animation after some repeats. The scenario is as following :
First animation starts from -25 to 25 degree, after one animating, this should be change to -24 to 24 and reversely and … and when reach to 0 to 0 this should be cancel.
int intervalSize = -25;
RotateAnimation r = new RotateAnimation(intervalSize, intervalSize, pivotX, pivotY);
r.setDuration(3000);
r.setStartOffset(0);
r.setRepeatMode(RotateAnimation.REVERSE);
r.setRepeatCount(RotateAnimation.INFINITE);
startAnimation(r);
r.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
if (intervalSize == 0)
animation.cancel();
intervalSize--;
}
@Override
public void onAnimationEnd(Animation animation) {
}
});
Could any one please how can I reach to this ?
Thanks in advance:)
I think your problem is that while you ARE changing your global var interval size upon each animation repeat, the animation that is being repeated is not looking at your var, but rather the primitive ints that were passed to it when you said: new RotateAnimation(intervalSize, intervalSize, pivotX, pivotY);
That is, the animation will always have -25 since that’s what it was constructed with, it doesnt care or know about the subsequent changes to your intervalSize var.
To achieve what you want ideally you’d be able to do something like:
But alas, it doesnt look like there are setter methods for those attributes on RotationAnimation. So that leaves you with the possibility of using the onAnimationEndEvent to create a new Animation with the new intervalSizeValue. As in:
Use onAnimationEnd event and rather than have a repeating animation, have a one time animation. Once it ends, the onAnimationEnd event should construct a new RotationAnimation with your new intervalSize.
Here is a working example that animates the textview from the typical Android HelloWorld app in the way you specify: