Android translate动画在制作动画时立即需要全高

Android translate动画在制作动画时立即需要全高,android,android-animation,Android,Android Animation,因此,假设您有一个父相对布局,其高度设置为wrap\u content。在这个布局中是另一个默认隐藏的布局。我是这样揭示的: view.startAnimation(AnimationUtils.loadAnimation(context, R.anim.translate_in)); view.setVisibility(View.VISIBLE); 下面是相应的动画文件。它基本上将视图从底部移动到顶部 <translate android:duration="250"

因此,假设您有一个父相对布局,其高度设置为
wrap\u content
。在这个布局中是另一个默认隐藏的布局。我是这样揭示的:

view.startAnimation(AnimationUtils.loadAnimation(context, R.anim.translate_in));
view.setVisibility(View.VISIBLE);  
下面是相应的动画文件。它基本上将视图从底部移动到顶部

 <translate
    android:duration="250"
    android:fromXDelta="0"
    android:fromYDelta="100%p"
    android:toXDelta="0"
    android:toYDelta="0"
    android:fillAfter="true"/>

我希望父视图根据子视图的动画更改其高度。但是,当动画开始时,父视图立即向上移动,而子视图仍在制作动画。感觉好像在动画完成之前调用了setVisibility

所以为了解决这个问题,我会将孩子的高度从0缩放到100%,但是我不想要缩放,而是平移效果

有没有办法解决这个问题

布局很简单:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:backgroundColor="@color/grey">

    <TextView/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundColor="@color/white"
        android:visibility="GONE">

        <TextView/>

     </RelativeLayout>

</RelativeLayout>


这听起来像是“隐藏”,你的意思是
消失了
,在这种情况下,父对象的布局就像子对象不在那里一样,并且最初的高度
0
,但一旦你将子对象设置为
可见
,父对象就会包装子对象的实际高度,并且“立即向上移动”。是吗?@MikeM。布局真的没有什么特别之处。查看我的编辑。听起来像是“隐藏”,你的意思是
消失了
,在这种情况下,父对象的布局就像子对象不在那里一样,并且最初的高度
0
,但一旦你将子对象设置为
可见
,父对象就会包装子对象的实际高度,并且“立即向上移动”。是吗?@MikeM。布局真的没有什么特别之处。查看我的编辑。它应该返回false,这样它就不会进行第一次绘制。它应该返回false,这样它就不会进行第一次绘制。
    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);