动画时,Android翻译动画立即需要全高 [英] Android translate animation instantly requires full height while animating

查看:84
本文介绍了动画时,Android翻译动画立即需要全高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,假设您有一个父相对布局,其高度设置为wrap_content.在此布局内是另一种布局,默认情况下是隐藏的.这就是我透露的方式:

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.startAnimation(AnimationUtils.loadAnimation(context, R.anim.translate_in));
view.setVisibility(View.VISIBLE);  

这是相应的动画文件.它基本上将视图从底部移到顶部.

Here is the corresponding anim file. It basically moves the view from the bottom to the top.

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

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

I expect the parent view to change its height according to the animation of the child view. However when the animation starts the parent view instantly shifts up while the child view is still animating. It feels like the setVisibility is called before the animation has finished.

因此,要解决此问题,我会将孩子的身高从0缩放到100%,但是我不希望缩放,而是想要平移效果.

So to solve this I would scale the height of the child from 0 to 100%, however I do not want a scale but a translate effect.

有什么办法解决这个问题吗?

Any ideas how to approach this problem?

布局很简单:

<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>

推荐答案

    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);

这篇关于动画时,Android翻译动画立即需要全高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆