I'm using RotateAnimation and i want to change the animation while it's running, for example, changing the "toDegrees" parameter and thous making the animation shorter or longer.
Thanks.
I'm using RotateAnimation and i want to change the animation while it's running, for example, changing the "toDegrees" parameter and thous making the animation shorter or longer.
Thanks.
You have cancel() startOffsetTime(), and restrictDuration(). I don't see any api to change the degree though, so you may have to start a new RotateAnitmation
The accepted answer didn't work for me. What's needed is 'smooth' continuation of the current animation without getting a glitch. Instead I modified the RotationAnimation class slightly to return the 'currently used' degrees, which can then be used:
/**
* Allows you to fetch the currently 'used' animation degrees so you can create a new animator
* out of it, allowing smooth animation
*/
class AdjustableRotationAnimation extends Animation {
private float mFromDegrees;
private float mToDegrees;
private volatile float mAnimatedDegrees;
private int mPivotXType = ABSOLUTE;
private int mPivotYType = ABSOLUTE;
private float mPivotXValue = 0.0f;
private float mPivotYValue = 0.0f;
private float mPivotX;
private float mPivotY;
public AdjustableRotationAnimation(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AdjustableRotationAnimation(float fromDegrees, float toDegrees) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mPivotX = 0.0f;
mPivotY = 0.0f;
}
public AdjustableRotationAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mPivotXType = ABSOLUTE;
mPivotYType = ABSOLUTE;
mPivotXValue = pivotX;
mPivotYValue = pivotY;
}
public AdjustableRotationAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,
int pivotYType, float pivotYValue) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mPivotXValue = pivotXValue;
mPivotXType = pivotXType;
mPivotYValue = pivotYValue;
mPivotYType = pivotYType;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
mAnimatedDegrees = mFromDegrees + ((mToDegrees - mFromDegrees) * interpolatedTime);
float scale = getScaleFactor();
if (mPivotX == 0.0f && mPivotY == 0.0f) {
t.getMatrix().setRotate(mAnimatedDegrees);
} else {
t.getMatrix().setRotate(mAnimatedDegrees, mPivotX * scale, mPivotY * scale);
}
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mPivotX = resolveSize(mPivotXType, mPivotXValue, width, parentWidth);
mPivotY = resolveSize(mPivotYType, mPivotYValue, height, parentHeight);
}
public synchronized float getAnimatedDegrees() {
return mAnimatedDegrees;
}
}
And this is how I use it:
currentRotate = currentRotate % DEGREES_360;
int animationDuration = 400;
if (rotateAnimation != null && !rotateAnimation.hasEnded()) {
rotateAnimation = new AdjustableRotationAnimation(rotateAnimation.getAnimatedDegrees(), currentRotate, Animation.RELATIVE_TO_SELF, CENTER, Animation.RELATIVE_TO_SELF, CENTER);
rotateAnimation.setInterpolator(new DecelerateInterpolator());
rotateAnimation.setDuration(animationDuration);
rotateAnimation.setFillAfter(true);
rotateAnimation.setAnimationListener(this);
} else {
rotateAnimation = new AdjustableRotationAnimation(lastRotation, currentRotate, Animation.RELATIVE_TO_SELF, CENTER, Animation.RELATIVE_TO_SELF, CENTER);
rotateAnimation.setInterpolator(new DecelerateInterpolator());
rotateAnimation.setDuration(animationDuration);
rotateAnimation.setFillAfter(true);
rotateAnimation.setAnimationListener(this);
}
viewToRotate.startAnimation(rotateAnimation);
This would create a new animation each time, however it would give the feeling of continuation and the animation will smoothly continue from where it got 'cancelled'.
This can help you (just added getters and setters to needed fields)