Android translate animation instantly requires full height while animating

后端 未结 1 1060
误落风尘
误落风尘 2021-01-29 01:00

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

相关标签:
1条回答
  • 2021-01-29 01:47
        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);
    
    0 讨论(0)
提交回复
热议问题