Kemix Kemix - 1 month ago 11
Android Question

Android: Stay in state of older animation when start a new animation

I would like to make two type of animation on a single view : a TranslateAnimation and a few moment later a ScaleAnimation. The problem is that when the ScaleAnimation begins the views return to their initial state (before the TranslateAnimation). Is there a way to do what I want ?

Answer

You can add a listener to the TranslateAnimation, and when it's done, change the position of the View to the animation's end position.

TranslateAnimation anim = new TranslateAnimation(...);
anim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) { }
@Override
public void onAnimationRepeat(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation) {
    view.setX(NEW_X_POSITION);
    view.setY(NEW_Y_POSITION);
}
});

But I'd suggest actually switching from the old TranslateAnimation/ScaleAnimation type of classes, to the new ViewPropertyAnimator framework (http://android-developers.blogspot.co.il/2011/05/introducing-viewpropertyanimator.html). In the new framework you don't have to change positions like in the above code, and the syntax is much simpler:

myView.animate().x(500).y(500);

you can also use AnimatorSet to create a set of animation to be played together or one after the other.

Further reading: https://developer.android.com/reference/android/view/ViewPropertyAnimator.html

Comments