折叠动画不会完全折叠

时间:2015-09-23 11:59:41

标签: android animation

我使用此代码折叠并展开视图:

public static void expandView(final View view)
    {
        view.measure(WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
        final int finalHeight = view.getMeasuredHeight();

        view.getLayoutParams().height = 1;
        view.setVisibility(View.VISIBLE);

        Animation animation = new Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t)
            {
                view.getLayoutParams().height =
                        interpolatedTime == 1 ? WindowManager.LayoutParams.WRAP_CONTENT :
                                (int) (finalHeight * interpolatedTime);
                view.requestLayout();
            }

            @Override
            public boolean willChangeBounds()
            {
                return true;
            }
        };

        animation.setDuration((long) (finalHeight /
                view.getContext().getResources().getDisplayMetrics().density));
        view.startAnimation(animation);
    }

    public static void collapseView(final View view)
    {
        final int initialSize = view.getMeasuredHeight();

        Animation animation = new Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t)
            {
                if (interpolatedTime == 1)
                    view.setVisibility(View.GONE);
                else
                {
                    view.getLayoutParams().height =
                            initialSize - (int) (initialSize * interpolatedTime);
                    view.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds()
            {
                return true;
            }
        };
        animation.setDuration((long) (initialSize /
                view.getContext().getResources().getDisplayMetrics().density));
        view.startAnimation(animation);
    }

我要扩展的视图如下所示:

<RelativeLayout
    android:id="@+id/expand"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/darker_green">

        <View
            android:layout_width="match_parent"
            android:layout_height="300dp"/>
    </LinearLayout>
</RelativeLayout>

将其设置为列表中的第一项。展开和折叠工作正常,但是当视图滚动屏幕并且我尝试折叠它时,视图不会整个崩溃,在滚动到顶部时我得到以下结果:

展开: Expanded 折叠: Collapsed

1 个答案:

答案 0 :(得分:0)

这是我使用2年的方法。试一试。

public static void collapse(final View v, long duration) {
    if(v == null)
        return;
    final int initialHeight = v.getMeasuredHeight();

    Animation a = new Animation()
    {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            if (interpolatedTime == 1) {
                v.setVisibility(View.GONE);
            } else {
                v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
                v.requestLayout();
            }
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    // 1dp/ms
    if (Long.valueOf(duration).equals(-1L)) {
        a.setDuration((int) (initialHeight / v.getContext().getResources().getDisplayMetrics().density));

    } else {
        a.setDuration(duration);
    }
    v.startAnimation(a);
}