Android动画视图不会调整其他视图的大小

时间:2018-07-20 11:49:40

标签: android view android-animation android-transitions objectanimator

我在Android上遇到问题,无法理解原因。我有以下布局:

<?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:id="@+id/Container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/background_primary">
            <RelativeLayout
                android:id="@+id/RecyclerViewContainer"
                android:layout_below="@+id/UpdatesBanner"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/RecyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:scrollbars="none"
                    android:clipToPadding="false"
                    android:splitMotionEvents="false"
                    android:requiresFadingEdge="none"
                    android:overScrollMode="never" />
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@android:color/holo_red_dark" />
            </RelativeLayout>
            <Droid.UIElements.UIBanner
                android:id="@+id/UpdatesBanner"
                android:layout_width="match_parent"
                android:layout_height="130dp"
                android:layout_alignParentTop="true"
                android:background="@android:color/holo_green_dark" />
        </RelativeLayout>
    </android.support.design.widget.CoordinatorLayout>

UIBanner是包含2个按钮的自定义视图。其中之一绑定到将对视图进行动画处理的Hide函数上。

 public void Hide()
    {
        ObjectAnimator animation = ObjectAnimator.OfFloat(this, "TranslationY", TranslationY, TranslationY - Height);
        animation.Start();
    }

这有效,但横幅下的视图保持相同大小且不会扩展。如果我仅在横幅上显示可见性,则它可以工作,但不执行动画。

以下是动画前后的状态:

Before animation

After animation

2 个答案:

答案 0 :(得分:0)

找到了解决方案,不知道这是否是实现此目标的最佳方法,我也必须为第二个视图设置动画。遵循代码:

ObjectAnimator bannerAnimation = ObjectAnimator.OfFloat(banner, "TranslationY", banner.TranslationY, banner.TranslationY - banner.Height);
ObjectAnimator viewAnimation = ObjectAnimator.OfFloat(recycler, "ScaleY", recycler.ScaleY, recycler.ScaleY - banner.Height);
AnimatorSet set = new AnimatorSet();
set.PlayTogether(bannerAnimation, viewAnimation);
set.Start();

答案 1 :(得分:0)

最好的解决方案是ValueAinmator

ValueAnimator viewAnim = ValueAnimator.OfInt(view.MeasuredHeight, viewHeight);
viewAnim.AddUpdateListener(new UpdateListener(view));
AnimatorSet set = new AnimatorSet();
set.AnimationEnd += (sender, e) => EndAnim();
set.Play(viewAnim);
set.Start();

还有听众

class UpdateListener : Java.Lang.Object, IAnimatorUpdateListener
{
     Android.Views.View view;

     public UpdateListener(Android.Views.View pView)
     {
         view = pView;
     }

     public void OnAnimationUpdate(ValueAnimator animation)
     {
         int value = (int)animation.AnimatedValue;
         ViewGroup.LayoutParams layoutParams = view.LayoutParameters;
         layoutParams.Height = value;
         view.LayoutParameters = layoutParams;
     }
}