Android property animation: how to increase view height?

How to increase the view height using Property Animations in Android?

ObjectAnimator a = ObjectAnimator.ofFloat(viewToIncreaseHeight, "translationY", -100);
a.setInterpolator(new AccelerateDecelerateInterpolator());
a.setDuration(1000);
a.start();

The translationY actually moves the view not increase the height. How can I increase the height of the view?


Solution 1:

ValueAnimator anim = ValueAnimator.ofInt(viewToIncreaseHeight.getMeasuredHeight(), -100);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        int val = (Integer) valueAnimator.getAnimatedValue();
        ViewGroup.LayoutParams layoutParams = viewToIncreaseHeight.getLayoutParams();
        layoutParams.height = val;
        viewToIncreaseHeight.setLayoutParams(layoutParams);
    }
});
anim.setDuration(DURATION);
anim.start(); 

Solution 2:

You can use ViewPropertyAnimator, which can save you some lines of code :

yourView.animate()
   .scaleY(-100f)
   .setInterpolator(new AccelerateDecelerateInterpolator())
   .setDuration(1000);

that should be all you need, be sure to check the documentation and all available methods for ViewPropertyAnimator.

Solution 3:

This is not a direct answer to this question. However, it may help someone.

Sometimes, we want to increase/decrease view's height because some of its child is is being added/removed (or just becoming visible/gone).

On those cases, you really can use Android default animation. As mentioned in other answers, you can set via:

<LinearLayout
    ...
    android:animateLayoutChanges="true"
.../>

Or in Java:

linearLayout.setLayoutTransition(new LayoutTransition());

If you created a custom view:

public StickerPickerView(final Context context, final AttributeSet attrs,
        final int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setLayoutTransition(new LayoutTransition());
}

For me, it was pretty satisfactory but I just tested on latest APIs. That will automatically add a fade in/out when your view becomes visible. It will also animate the height/width of your view when same changes (like when you change the visibility of one of the child views etc).

So, I recommend to at least give a try.