Reputation: 534
I have a View
to which I have added a RotateAnimation
and it is working perfectly. The only question that I have is that, is there a way to listen to every angle change. I know I can add an AnimationListener
and use the following listeners,
rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
And also I can use View.getRotation()
to get the angle of the view. But the functionality of the app changes w.r.t time calculated from the preferences selected by the user.
What I am looking for is a listener which can listen to every angle change.
Something like, onRotateAngleChange(int currentAngle)
.
Any pointers on any such methods or any workaround that I can use would be a great help.
Regards.
EDIT : Final Hint/Code that worked. Thanks to @yakobom for the pointer.
rotateAnimation = new RotateAnimation(fromDegrees, toDegrees){
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
float degrees = fromDegrees + ((toDegrees - fromDegrees) * interpolatedTime);
//Do what you want to do with your angle here.
}
};
Upvotes: 1
Views: 679
Reputation: 2711
All you need to do is implement applyTransformation
, something like this:
RotateAnimation rotateAnim = new RotateAnimation (...) {
@Override
public void applyTransformation(float interpolatedTime, Transformation t)
{
super.applyTransformation(interpolatedTime, t);
<your code here>
}
Upvotes: 2
Reputation: 1620
It's not possible to add an update listener for RotationAnimation
however you can achieve the same result by using an ObjectAnimator
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", initialValue, finalValue);
animator.setDuration(duration);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
currentRotation = valueAnimator.getAnimatedValue("rotation");
//do something
}
});
Alternatively if you're targeting API 19 or higher you can use the Animation apis in Kitkat added to make this much simpler as follows:
view.animate().rotationBy(finalValue - initalValue).setUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
currentRotation = valueAnimator.getAnimatedValue("rotation");
//do something
}
});
Upvotes: 0