Jacek Kwiecień Jacek Kwiecień - 3 months ago 13
Android Question

Returning view rotation to it's original state after canceling animation in API < 11

I'm running shake animation on my view:

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="100"
android:fromDegrees="-5"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toDegrees="5" />


I'm canceling animation on user demand, but the view stays rotated as it was in the moment of stopping and I wan't it to back to it's original state.

Here is how I'm stopping the animation:

if (animation != null) {
animation.cancel();
animation.reset();
}


I can't use
setRotation()
because my min API is 9.

How can I achieve that?

Answer

I used this method (code is inside the class inheriting from View):

private void returnToOriginalRotationState() {
    RotateAnimation animation = new RotateAnimation(0.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
    animation.setInterpolator(new LinearInterpolator());
    animation.setDuration(1);
    animation.setFillAfter(true);

    startAnimation(animation);
}
Comments