Android - Rotate animation with Zoom effects


    Android's object animation is quite powerful to control different view attributes. The only catch is that complex animations needs code changes and just can't be done via xml alone. In this case, the animation is to rotate an image along Y axis along with a zoom effects. The zoom works half way through the animation and goes the other way.



    Its possible to achieve this via both rotation and scale animation. However, this illustration is just to demonstrate the different possible customizations via property animation. The idea is to just listen for Animation Updates provided by ValueAnimator (base of ObjectAnimator) and use this update's AnimatedFraction to calculate desired values during the animation, padding in this case. The animated fraction varies from 0 to 1 for all kinds of animation. A value of 0.5 indicates that the animation is half done.


        final int animationPadding = 100;

        ObjectAnimator animator = ObjectAnimator.ofFloat(mView, "rotationY", 0, 360);
        animator.setDuration(2000);

        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {

                float animatorFactor = valueAnimator.getAnimatedFraction();
                float paddingFactor = animatorFactor > 0.5 ? (1 - animatorFactor) : animatorFactor;
                int padding = (int) (paddingFactor * animationPadding * 2);

                mView.setPadding(padding, padding, padding, padding);

            }
        });

        animator.start();

No comments: