Skip to content Skip to sidebar Skip to footer

Animation Of Height Of Linearlayout Container With Valueanimator

I have a LinearLayout that I use as a container for some buttons and textview's that I would like to animate the height of to give an impression of the layout sliding down when the

Solution 1:

Looking at this blog post: http://tech.chitgoks.com/2011/10/29/android-animation-to-expand-collapse-view-its-children/ I found that I shouldn't use view.invalidate() to have the layout redrawn. I should use view.requestLayout().

Thus the code becomes something like this:

ValueAnimatorva= ValueAnimator.ofInt(0, height);
    va.setDuration(700);
    va.addUpdateListener(newValueAnimator.AnimatorUpdateListener() {
        publicvoidonAnimationUpdate(ValueAnimator animation) {
            Integervalue= (Integer) animation.getAnimatedValue();
            v.getLayoutParams().height = value.intValue();
            v.requestLayout();
        }
    });

I just wanted to add a note on how to get the height of the LinearLayout as well to make the animation dynamic. To get the height all the views need to be drawn first. Therfor we have to listen for an event that tells us that the drawing is done. This can be done from the onResume() method like this (note that in my xml I have declared the container to wrap_content for height and it is also visible, since I want to hide it from the start I do that efter measuring it):

@OverridepublicvoidonResume() {
        super.onResume();
        finalViewTreeObservervto= filterContainer.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(newOnGlobalLayoutListener() {
            @OverridepublicvoidonGlobalLayout() {
                height = filterContainer.getHeight();
                filterContainer.setVisibility(View.GONE);
                filterContainer.getLayoutParams().height = 0;
                filterContainer.requestLayout();
                ViewTreeObserverobs= filterContainer.getViewTreeObserver();
                obs.removeGlobalOnLayoutListener(this);
            }
        });
    }

Post a Comment for "Animation Of Height Of Linearlayout Container With Valueanimator"