I want to create a custom contentScrim for my collapsingToolbarLayout
. My collapsingToolbarLayout has an imageview
inside. When it scrolls, in theory the imageview is suppose to fade out and my scrim color is suppose to fade in.
We all know we can create a scrim like this:
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="?attr/colorPrimary"
android:background="@color/white"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp"
android:fitsSystemWindows="true"
app:expandedTitleTextAppearance="@color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
>
However the android default scrim only seems to start working when you scroll 3/4 up the screen. Before you that point, the imageview is still fully shown. Also when you reach 3/4 of the screen, the scrim kicks in and automatically changes the color of the collapsingtoolbarlayout to your scrim color:
Instead of having it fill the screen fully when you scroll 3/4 of the way up and before we reached the toolbar
, I want it to fade gently till your scrolled up to the toolbar
.
If you want to see an example of the effect I want to create, have a look at the Facebook app. This is the Facebook app when the collapsingToolbarLayout is fully expanded:
When you scroll to about 3/4 of the way down, the collapsingToolbarLayout has a faded blue color and the blue is not completely saturating:
So I have create the following inside my collapsingToolbarLayout
:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/frame_picture">
<com.customshapes.SquareImageView
android:id="@+id/backdrop"
android:contentDescription="@string/photo"
android:transitionName="@string/transition"
android:layout_width="match_parent"
android:layout_height="240dp"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
/>
<com.customshapes.SquareImageView
android:id="@+id/fading_backdrop"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:layout_height="240dp"
android:alpha="0"
android:fitsSystemWindows="true"
/>
</FrameLayout>
The framelayout comprises of the backdrop imageview
which holds the picture that is displayed inside my collapsingToolbarLayout and in front of it is an imageview which just holds the 'scrimColor' ?attr/colorPrimary with an alpha of 0 so that the backdrop imageview
will shine through.
Then I implemented the appBarLayout's onOffsetChangedListener:
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
fading_backdrop.setImageAlpha(verticalOffset);
fading_backdrop.invalidate();
}
We are setting the alpha of the fading_backdrop so that it will appear when we scroll up. I'm trying to create a scrim artifically.
However, this doesn't seem to work properly. There is no fading at all when I run this code. Does anyone know what I'm doing wrong?