使用折叠工具栏动画动画卡片视图/布局

Animating Card View / Layout with Collapsing toolbar animation

我一直在使用代码路径指南具体化我的应用程序。我成功地实现了折叠工具栏,但我不知道如何做这个动画效果> enter image description here

随着折叠工具栏的布局更改为卡片视图列表或仅更改背景色 change.How 来做到这一点?到现在我还没有找到任何东西。是否有可用的库或我必须进行自定义行为? 到目前为止,这是我的布局 xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:weightSum="1">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff995B00"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="#FF9800"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleTextAppearance="@style/TransparentText"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="100dp"
                android:scaleType="centerCrop"
                android:src="@drawable/drawer_background"
                app:layout_collapseMode="parallax"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:elevation="4dp"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nested_scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffffff"
            android:orientation="vertical">

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:elevation="4dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">


                    <TextView
                        android:id="@+id/about1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentTop="true"
                        android:maxLines="3"
                        android:padding="8sp"
                        android:text="App Details"
                        android:textColor="#ff333333"
                        android:textSize="10sp" />

                    <ProgressBar
                        android:id="@+id/about2"
                        style="?android:attr/progressBarStyleHorizontal"
                        android:layout_width="fill_parent"
                        android:layout_height="1dp"
                        android:layout_below="@+id/about1"
                        android:background="#ffdddddd" />

                    <TextView
                        android:id="@+id/about3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/about2"
                        android:maxLines="3"
                        android:padding="8sp"
                        android:text="Version 0.2.a"
                        android:textColor="#666"
                        android:textSize="17sp" />

                    <TextView
                        android:id="@+id/about4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/about3"
                        android:maxLines="3"
                        android:padding="8sp"
                        android:text="Albigo Alpha Build 2"
                        android:textColor="#666"
                        android:textSize="17sp" />

                    <TextView
                        android:id="@+id/about5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/about4"
                        android:maxLines="3"
                        android:padding="8sp"
                        android:text="SR Labs™  ©2015-2016"
                        android:textColor="#666"
                        android:textSize="17sp" />

                    <TextView
                        android:id="@+id/about6"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/about5"
                        android:maxLines="3"
                        android:padding="8sp"
                        android:text="All rights reserved"
                        android:textColor="#666"
                        android:textSize="17sp" />


                </RelativeLayout>

            </android.support.v7.widget.CardView>

        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/changelog_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@drawable/changelog"
        app:backgroundTint="#FF9800"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|right|end"
        app:rippleColor="#FFF" />
</android.support.design.widget.CoordinatorLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/drawer_recyclerView"
    android:layout_width="275dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#FFFFFFFF"
    android:scrollbars="vertical" />

如果它是一个 CardView 的问题,很容易将 OnOffsetChangeListener 绑定到 AppBarLayout 并在 CardView 折叠时提升它。此外,您还需要使用 ArgbEvaluator 更改 CardView 的颜色(从父视图颜色到白色)以实现从父视图上升的效果。

public class CardViewElevationOffsetListener implements AppBarLayout.OnOffsetChangedListener {
    private final CardView mCardView;
    private float mTargetElevation;
    private int mToolbarHeight;
    public CardViewElevationOffsetListener(Toolbar toolbar, CardView cardView) {
        mToolbarHeight = toolbar.getHeight();
        mCardView = cardView;
        mTargetElevation = ViewCompat.getElevation(cardView);
        ViewCompat.setElevation(cardView, 0);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int offset) {
        offset = Math.abs(offset);
        if (offset >= appBarLayout.getTotalScrollRange() - mToolbarHeight) {
            float flexibleSpace = appBarLayout.getTotalScrollRange() - offset;
            float ratio = 1 - (flexibleSpace / mToolbarHeight);
            float elevation = ratio * mTargetElevation;
            ViewCompat.setElevation(mCardView, elevation);
        } else {
             ViewCompat.setElevation(mCardView,0);
        }

    }    

}