Search code examples
android-animationrotateanimation

multiple RotationAnimations (time shifted) using false angles


i am trying a quite simple animation. rotate a view to the left for 18°, then rotate it to the right for 180°(relative) (+162°absolute) and then back to normal 0°(absolute) (-162°relative)...

int first = -18;
int second = first +180;
int animationGap = 500;
//left rotation
final RotateAnimation leftRotation = new RotateAnimation(0, first, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
leftRotation.setDuration(1000);
leftRotation.setInterpolator(new AccelerateDecelerateInterpolator());
//right rotation
final RotateAnimation rightRotation = new RotateAnimation(first, second, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rightRotation.setDuration(1000);
rightRotation.setStartOffset(1000+animationGap);
rightRotation.setInterpolator(new AccelerateDecelerateInterpolator());
//back to normal rotation
final RotateAnimation toNormalRotation = new RotateAnimation(second, 0, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
toNormalRotation.setDuration(1000);
toNormalRotation.setStartOffset(2000+animationGap*2);
toNormalRotation.setInterpolator(new AccelerateDecelerateInterpolator());
//animation set
final AnimationSet animationSet = new AnimationSet(true);
animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
animationSet.addAnimation(leftRotation);
animationSet.addAnimation(rightRotation);
animationSet.addAnimation(toNormalRotation);

and i start the whole bundle with this:

view.startAnimation(animationSet);

it is working as expected with first= -90; but is totally wrong with the upper constellation... (tested on samsung s4 and google nexus s)
of course i can quick fix this with AnimationListener and starting the next animation from it but this is no real solution or answer.
so is there anyone who can tell my why this is happening and how i can prevent this?
edit:
i think the problem could be the underlaying mathematical representation as a martix (rotations may manipulate each otehr). but it should not be a problem because android could recognize this or may generate different ones because of the time delay between.
i'm still looking for a nice solution


Solution

  • my guess was right and so the only solution is using AnimationListener for each single rotation and starting the next in the onEnd method..