Interrupt translate animation and start another
-
26-10-2019 - |
题
I have an application where I need to adjust view position according to current focused subview (it's a list with focusable items, current focused item must be in the center of the screen - used for a tv application controlled from tv remote).
Position must be adjusted with animation.
I got it working with just one problem: if the user changes focus before the animation is complete (quickly taps "up" button twice) next animation starts with "jump" - it starts from the same position as did the first one.
So what I try to do is I cancel previous animation and start another, but then new animation starts from the point where first animation was started, so user can see a very obvious jumps in the animation, which look really awful.
Here's the code:
@Override
public void requestChildFocus(final View child, final View focused) {
super.requestChildFocus(child, focused);
//this test code included for explanation
Rect r = new Rect();
child.getDrawingRect(r); //this will return view's position ignoring animation state
Rect r2 = new Rect();
child.getGlobalVisibleRect(r2); //as will this one too
Log.d("Top: " + child.getTop() + "; Drawing rect: " + r.toString() + "; global visible rect: " + r2.toString());
//all of this methods will ignore changes that were made
//by animation object - they'll return numbers from LayoutParam
//calculate current position inside view and position to move to
//cursorOffset - is the "center" of the screen
final int currentPosition = child.getTop();
final int requaredPosition = cursorOffset - focused.getTop();
//cancel current running animation - layout params will not change
//if i do change layout params when cancelling animation, it looks even worse
//because of jumping back list jumps forward
if (currentAnimation != null) {
Animation animation = currentAnimation;
currentAnimation = null;
animation.cancel();
}
//just a regular translate animation
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, requaredPosition - currentPosition);
animation.setDuration(300);
animation.setFillEnabled(true);
animation.setFillBefore(true);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
currentAnimation = animation;
}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
if (animation == currentAnimation) {
//change layout params if animation finished running (wasn't cancelled)
RelativeLayout.LayoutParams params = (LayoutParams) child.getLayoutParams();
params.setMargins(0, requaredPosition, 0, 0);
child.setLayoutParams(params);
}
}
});
child.startAnimation(animation);
}
So must question is: how can I start translate animation from the point where previous translate animation left on (assuming that it was cancelled)?
Or, in a simplier words, how can I determine current visible rect of a view?
解决方案
Apparently you can't get current view position, but you can get current animation state.
So you can get current y offset by doing this:
Transformation transformation = new Transformation();
float[] matrix = new float[9];
currentAnimation.getTransformation(AnimationUtils.currentAnimationTimeMillis(), transformation);
transformation.getMatrix().getValues(matrix);
float y = matrix[Matrix.MTRANS_Y];
And that's how I was able to cancel one animation and start another one exactly from the point where I left of. If anyone cares, heres the complete code:
private Animation currentAnimation;
private float[] matrix = new float[9];
private Transformation transformation = new Transformation();
@Override
public void requestChildFocus(final View child, final View focused) {
super.requestChildFocus(child, focused);
final int currentPosition;
if (currentAnimation != null) {
currentAnimation.getTransformation(AnimationUtils.currentAnimationTimeMillis(), transformation);
transformation.getMatrix().getValues(matrix);
float y = matrix[Matrix.MTRANS_Y];
RelativeLayout.LayoutParams params = (LayoutParams) child.getLayoutParams();
params.topMargin += y;
//child.getTop() will return wrong position until layout actually happens,
//so I use params.topMargin as a current position in case I need to cancel
currentPosition = params.topMargin;
child.requestLayout();
currentAnimation.setAnimationListener(null);
currentAnimation.cancel();
currentAnimation = null;
} else {
currentPosition = child.getTop();
}
final int requaredPosition = cursorOffset - focused.getTop();
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, requaredPosition - currentPosition);
animation.setDuration(300);
animation.setFillEnabled(true);
animation.setFillBefore(true);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
currentAnimation = animation;
}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
if (animation == currentAnimation) {
RelativeLayout.LayoutParams params = (LayoutParams) child.getLayoutParams();
params.setMargins(0, requaredPosition, 0, 0);
child.requestLayout();
}
currentAnimation = null;
}
});
child.startAnimation(animation);
}
Hope somebody finds this useful.
其他提示
For everybody that searches for help to create a clock or a speedometer like me (look for RotationAnimation < API lvl 9), I found out, that it is a good idea to set the AnimationListener and save the animation state in a boolean variable. Before you start a new animation check the state. So your animation will run until the end and look very smoothly. I am sorry for my english.
if(isAnimationActive){
//[...]
rotate = new RotateAnimation(oldRotateDegree, rotateDegree, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(rotateDuration);
rotate.setFillAfter(true);
rotate.setAnimationListener(this);
}
//[...]
@Override
public void onAnimationEnd(Animation animation)
{
this.isAnimationActive = false;
}
@Override
public void onAnimationStart(Animation animation)
{
this.isAnimationActive = true;
}