androidandroid-animationobjectanimator

How to make a Scale Up Animation with Object Animator


When using anim, I could do something like this

ScaleAnimation animation = new ScaleAnimation(0, 1.0, 0, 1.0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

to scale from 0 to original size of the object.

How could I do the same with ObjectAnimator or ValueAnimator?


Solution

  • You could use something like this for ValueAnimator:

    ValueAnimator translate = ValueAnimator.ofFloat(1f, 1.5f);
    translate.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float scale = Float.parseFloat(animation.getAnimatedValue().toString());
            yourView.setScaleX(scale);
            yourView.setScaleY(scale);
        }
    });
    translate.start();
    

    And something like this for ObjectAnimator:

    AnimatorSet animationSet = new AnimatorSet();
    
    ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 1.5f);
    ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 1.5f);
    
    animationSet.playTogether(scaleX, scaleY);
    animationSet.start();
    

    You can also set duration/interpolator/delay and similar properties for both animations. Also do not forget to start the animation after configuring.

    NOTE: Did not test this code, something might not work properly.