Skip to content Skip to sidebar Skip to footer

Android Translate Animation Instantly Requires Full Height While Animating

So imagine you have a parent relative layout with height set to wrap_content. Inside this layout is another layout which is hidden by default. This is how I reveal it: view.startAn

Solution 1:

    final RelativeLayout parent = null;//parent relative layout
    final View child = null;//parent relative layout child

    final int initParentHeight=parent.getHeight();

    parent.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            parent.getViewTreeObserver().removeOnPreDrawListener(this);

            int finalParentHeight = parent.getHeight();//get parent final height
            final ViewGroup.LayoutParams layoutParams = parent.getLayoutParams();
            ValueAnimator animator = ValueAnimator.ofInt(initParentHeight, finalParentHeight);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    layoutParams.height = (int) animation.getAnimatedValue();
                    parent.requestLayout();//animate height change
                }
            });
            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;//reset parent layoutparams height to wrap_content
                    parent.requestLayout();
                }
            });
            animator.setDuration(250);
            animator.start();
            return true;
        }
    });
    child.setVisibility(View.VISIBLE);

Post a Comment for "Android Translate Animation Instantly Requires Full Height While Animating"