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

时间:2015-11-30 13:34:55

标签: animation android-cardview android-coordinatorlayout android-collapsingtoolbarlayout

我一直在使用codepath指南实现我的应用。我成功实现了折叠工具栏,但我还没有得到如何做这个动画效果> enter image description here

与折叠工具栏一起,布局更改为cardview列表或仅改变背景颜色。如何做到这一点?到目前为止我还没找到任何东西。是否有可用的库或我必须进行自定义行为? 这是我的布局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" />

1 个答案:

答案 0 :(得分:0)

如果只有一个CardView的问题,很容易将OnOffsetChangeListener绑定到AppBarLayout并在CardView崩溃时提升CardView。您还需要使用ArgbEvaluator(从父视图颜色更改为白色)更改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); } } } 的颜色,以便从父视图中获得上升效果。

div {
  background-color: rgba(10, 10, 10, .2);
}
/*Containers*/

#spotlightContainer {
  position: relative;
  height: 200px;
  width: 646px;
  padding: 10px 20px 0px 20px;
  background-color: rgba(0, 0, 0, .4);
}
#spotlightContent {
  width: 646px;
  height: auto;
  padding: 30px 0px 0px 0px;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
}
#spotlightTitle {
  width: auto;
  height: auto;
  display: inline-block;
  bottom: 0px;
}
/*CTA*/

#spotlightCTA {
  position: relative;
  display: inline-block;
  width: 300px;
  text-align: right;
  cursor: pointer;
  right: 0px;
  float: right;
}
#spotlightCTA span {
  font-family: "Open Sans", "Helvetica Neue", helvetica, arial, sans-serif;
  font-weight: 300;
  color: #fff;
  display: inline-block;
  padding: 5px 0px 0px 0px;
}
#spotlightCTA button {
  background-color: #ffd200;
  border-radius: 0px;
  border: none;
  margin: 0px 0px 0px 15px;
  padding: 5px;
  vertical-align: text-bottom;
}
.title {
  font-family: "Oswald", "Helvetica Neue", helvetica, arial, sans-serif;
  color: #fff;
  font-weight: 600;
  max-width: 400px;
  font-size: 32px;
  line-height: 1.2;
  display: inline-block;
}