views:

509

answers:

1

I have to animate a view from state A to B with changes to its scale, position and scrolling.

The following code almost does the trick:

AnimationSet animation = new AnimationSet(true);

int fromXDelta = view.getScrollX();
int fromYDelta = view.getScrollY();
view.scrollTo(0, 0);
float scale = (float) widthB / (float) widthA;
// Calculate toXDelta and toYDelta

TranslateAnimation translateAnimation = new TranslateAnimation(-fromXDelta, -toXDelta, -fromYDelta, -toYDelta);
translateAnimation.setDuration(duration);
animation.addAnimation(translateAnimation);

ScaleAnimation scaleAnimation = new ScaleAnimation(1, scale, 1, scale);
scaleAnimation.setDuration(duration);
animation.addAnimation(scaleAnimation);
animation.setAnimationListener(new AnimationListener() {

    @Override
    public void onAnimationEnd(Animation arg0) {
        view.clearAnimation();
        // Change view to state B by modifying its layout params and scroll
    }

    @Override public void onAnimationRepeat(Animation arg0) {}
    @Override public void onAnimationStart(Animation arg0) {}
});
view.startAnimation(animation);

The logic of onAnimationEnd makes the view flicker after the animation ends. Also if I don't call clearAnimation() the animation does not work as expected (why?).

Is this the right way to do this?

Thanks!

A: 

I don't know if it is the right way, but I was having the same problem as you. I managed to get rid of the flickering by adding a:

animation.reset();

Just before the:

view.clearAnimation();

Note: In your code, it would be arg0.reset();

Rui