【问题标题】:Android translate animation instantly requires full height while animatingAndroid 翻译动画在动画时立即需要全高度
【发布时间】:2016-04-21 15:47:24
【问题描述】:

假设您有一个高度设置为wrap_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>

【问题讨论】:

  • 这听起来像是“隐藏”,你的意思是GONE,在这种情况下,父母的布局就像孩子不在那里一样,最初的高度是0,但很快当您将孩子设置为VISIBLE 时,父母会包裹孩子的实际高度,并“立即向上移动”。是吗?
  • @MikeM。布局真的没有什么特别的。查看我的编辑。

标签: android android-animation


【解决方案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);

【讨论】:

  • 它应该返回 false 以便它不会进行第一次绘制。
  • 我知道这是旧的,但最好能了解一下这段代码的作用
猜你喜欢
  • 2012-04-13
  • 2011-09-18
  • 1970-01-01
  • 2013-10-16
  • 2021-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多