user1908375
user1908375

Reputation: 1089

CollapsingToolbarLayout define animation start

How could I change the starting of animation in CollapsingToolbarLayout? I figured out that animation is controlled by the app:contentScrim attribute. But the "alpha" animation is starting too soon. Could I change the animation attributes somehow? I guess by the height so about "200dp".

For example, if I would define <CollapsingToolbarLayout... layout_height="150dp" > I don't even see the image because it's already hidden by the animation at the beginning. Here is my xml:

<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/paralax_tabs_collapse_toolbar"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:background="@color/my_action_bar_color"
    android:fitsSystemWindows="true"
    app:contentScrim="?attr/colorPrimary"
    app:expandedTitleMarginStart="10dp"
    app:layout_scrollFlags="scroll|exitUntilCollapsed">

    <ImageView
        android:id="@+id/paralax_tabs_header_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:scaleType="centerCrop"
        app:layout_collapseMode="parallax" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/paralax_tabs_toolbar"
        android:layout_width="match_parent"
        android:layout_height="104dp"
        android:gravity="top"
        android:minHeight="?attr/actionBarSize"
        app:layout_collapseMode="pin"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:titleMarginTop="13dp" />

    <android.support.design.widget.TabLayout
        android:id="@+id/paralax_tabs_tabs"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_gravity="bottom"
        android:background="@color/app_tab_backgorund"
        app:tabIndicatorColor="@android:color/white"
        app:tabIndicatorHeight="5dp"
        app:tabMode="scrollable" />
</android.support.design.widget.CollapsingToolbarLayout>

Upvotes: 0

Views: 987

Answers (3)

Ultimo_m
Ultimo_m

Reputation: 4897

Based on the documentation you need to call the method on CollapsingToolbarLayout

setScrimVisibleHeightTrigger(int)

android.support.design:scrimVisibleHeightTrigger

Specifies the amount of visible height in pixels used to define when to trigger a scrim visibility change.

For support design library version: 26.1.0

Upvotes: 0

astro
astro

Reputation: 1

  1. Create you own CollapsingToolbarLayout.
  2. Override next functions: setScrimsShown(show/hide content scrim),onAttachedToWindow and onDetachedFromWindow.
  3. In onAttachedToWindow create AppBarLayout.OnOffsetChangedListener and add to the parent.In onDetachedFromWindow remove listener.(works only if parent is AppBarLayout).
  4. In setScrimsShown set the rules to start animation.

Example:

public class MyCollapsingLayout extends CollapsingToolbarLayout {
    private int mCurrentVOffset = 0;
    private AppBarLayout.OnOffsetChangedListener mListener = null;

    ...

    @Override
    public void setScrimsShown(boolean shown) {
        boolean show = /*your code */

        super.setScrimsShown(show);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        final ViewParent parent = getParent();

        if (parent instanceof AppBarLayout) {
            mListener = new AppBarLayout.OnOffsetChangedListener() {
                @Override
                public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                    mCurrentVOffset = verticalOffset;
                }
            };
            ((AppBarLayout) parent).addOnOffsetChangedListener(mListener);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        final ViewParent parent = getParent();
        if (mListener != null && parent instanceof AppBarLayout) {
            ((AppBarLayout) parent).removeOnOffsetChangedListener(mListener);
        }

        super.onDetachedFromWindow();
    }
}

Upvotes: 0

anthorlop
anthorlop

Reputation: 1881

Im afraid the solution is not easy.

I've had to create my own CollapsingToolbarLayout changing the original.

Original: https://android.googlesource.com/platform/frameworks/support/+/6ba61c5/design/src/android/support/design/widget

In my case I changed the getScrimTriggerOffset method

From:

final int getScrimTriggerOffset() {
    return 2 * ViewCompat.getMinimumHeight(this);
}

to:

final int getScrimTriggerOffset() {
    return 3 * ViewCompat.getMinimumHeight(this);
}

This method is used in line 677 of CollapsingToolbarLayout to determine if show or hide the content scrim.

// Show or hide the scrims if needed
if (mContentScrim != null || mStatusBarScrim != null) {
     if (getHeight() + verticalOffset < getScrimTriggerOffset() + insetTop) {
         showScrim();
     } else {
         hideScrim();
     }
}

I tried to extend CollapsingToolbarLayout and override this method but its declared as final.

I'm sorry for the late reply, hope it works

Upvotes: 2

Related Questions