Animating translation and scaling of view in Android

Posted by hgpc on Stack Overflow See other posts from Stack Overflow or by hgpc
Published on 2010-04-11T14:16:19Z Indexed on 2010/04/11 14:23 UTC
Read the original article Hit count: 282

Filed under:
|
|

I have to animate a view from state A to B with changes to its scale, position and scrolling. I know everything about state A (widthA, heightA, topA, leftA, scrollXA, scrollYA) and state B (widthB, heightB, topB, leftB, scrollXB, scrollYB).

So far I wrote the following code:

AnimationSet animation = new AnimationSet(true);

int toXDelta; // What goes here?
int toYDelta; // What goes here?

TranslateAnimation translateAnimation = new TranslateAnimation(1, toXDelta, 1, toYDelta);
translateAnimation.setDuration(duration);
animation.addAnimation(translateAnimation);

float scale = (float) widthB / (float) widthA;
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
    }

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

Is this the right way to do this?

If so, how should I calculate the values of toXDelta and toYDelta? I'm having trouble finding the exact formula.

Thanks!

© Stack Overflow or respective owner

Related posts about android

Related posts about animation