Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Add "autoAnimate" attribute to configure how scrim and title behave w…
Browse files Browse the repository at this point in the history
…hen collapsing the toolbar.

none - nothing will be auto-animated, i.e. values change gradually as you scroll
title - the title blending will be auto-animated when the scrim height has been reached
scrim - scrim will be auto-animated when the scrim height has been reached (this is the default)
all - title and scrim will be auto-animated when the scrim height as been reached

Note there is another value "children" which requires the changes of another branch to work
  • Loading branch information
dmfs committed Apr 7, 2017
1 parent 6838d8e commit 7f55603
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 16 deletions.
5 changes: 5 additions & 0 deletions demo/src/main/res/layout/activity_demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:title="@string/title"
app:expandedTitleTextAppearance="@style/TextAppearance.ExpandedTitle"
app:autoAnimate="all"
app:contentScrim="#ff00ff"
app:maxLines="3">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ final class CollapsingTextHelper {
private float mExpandedTextBlend;
private float mExpandedFirstLineDrawX;
private int maxLines = 3;
private float mBlendRatio;
// END MODIFICATION

public CollapsingTextHelper(View view) {
Expand Down Expand Up @@ -341,6 +342,25 @@ void setExpansionFraction(float fraction) {
}
}

// BEGIN MODIFICATION by dmfs: setter method for blend ratio
/**
* Set the value indicating the current scroll value. This decides how much of the
* background will be displayed, as well as the title metrics/positioning.
*
* A value of {@code 0.0} indicates that the expanded title is fully opaque.
* A value of {@code 1.0} indicates that the collapsed title is fully opaque.
*/
void setBlendRatio(float ratio) {
ratio = MathUtils.constrain(ratio, 0f, 1f);

if (ratio != mBlendRatio) {
mBlendRatio = ratio;
calculateCurrentOffsets();
}
}
// END MODIFICATION by dmfs


final boolean setState(final int[] state) {
mState = state;

Expand Down Expand Up @@ -371,10 +391,10 @@ float getExpandedTextSize() {
}

private void calculateCurrentOffsets() {
calculateOffsets(mExpandedFraction);
calculateOffsets(mExpandedFraction, mBlendRatio);
}

private void calculateOffsets(final float fraction) {
private void calculateOffsets(final float fraction, final float blendRatio) {
interpolateBounds(fraction);
mCurrentDrawX = lerp(mExpandedDrawX, mCollapsedDrawX, fraction,
mPositionInterpolator);
Expand All @@ -384,18 +404,20 @@ private void calculateOffsets(final float fraction) {
setInterpolatedTextSize(lerp(mExpandedTextSize, mCollapsedTextSize,
fraction, mTextSizeInterpolator));

// BEGIN MODIFICATION: set text blending
setCollapsedTextBlend(1 - lerp(0, 1, 1 - fraction, AnimationUtils
// BEGIN MODIFICATION: set text blending, using the blend ratio
setCollapsedTextBlend(1 - lerp(0, 1, 1 - blendRatio, AnimationUtils
.FAST_OUT_SLOW_IN_INTERPOLATOR));
setExpandedTextBlend(lerp(1, 0, fraction, AnimationUtils
setExpandedTextBlend(lerp(1, 0, blendRatio, AnimationUtils
.FAST_OUT_SLOW_IN_INTERPOLATOR));
// END MODIFICATION

if (mCollapsedTextColor != mExpandedTextColor) {
// If the collapsed and expanded text colors are different, blend them based on the
// fraction
// BEGIN MODIFICATION by dmfs: apply blend ratio
mTextPaint.setColor(blendColors(
getCurrentExpandedTextColor(), getCurrentCollapsedTextColor(), fraction));
getCurrentExpandedTextColor(), getCurrentCollapsedTextColor(), blendRatio));
// END MODIFICATION by dmfs
} else {
mTextPaint.setColor(getCurrentCollapsedTextColor());
}
Expand Down Expand Up @@ -756,7 +778,7 @@ private void ensureExpandedTexture() {
|| TextUtils.isEmpty(mTextToDraw)) {
return;
}
calculateOffsets(0f);
calculateOffsets(0f, 0f);

// BEGIN MODIFICATION: Calculate width and height using mTextLayout and remove
// mTextureAscent and mTextureDescent assignment
Expand Down Expand Up @@ -785,7 +807,7 @@ private void ensureCollapsedTexture() {
|| TextUtils.isEmpty(mTextToDraw)) {
return;
}
calculateOffsets(0f);
calculateOffsets(0f, 0f);
final int w = Math.round(mTextPaint.measureText(mTextToDraw, 0, mTextToDraw.length()));
final int h = Math.round(mTextPaint.descent() - mTextPaint.ascent());
if (w <= 0 && h <= 0) {
Expand All @@ -806,7 +828,7 @@ private void ensureCrossSectionTexture() {
|| TextUtils.isEmpty(mTextToDraw)) {
return;
}
calculateOffsets(0f);
calculateOffsets(0f, 0f);
final int w = Math.round(mTextPaint.measureText(mTextToDraw, mTextLayout.getLineStart(0),
mTextLayout.getLineEnd(0)));
final int h = Math.round(mTextPaint.descent() - mTextPaint.ascent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ public class CollapsingToolbarLayout extends FrameLayout {

private static final int DEFAULT_SCRIM_ANIMATION_DURATION = 600;

public static final int AUTO_ANIMATE_SCRIM = 1;
public static final int AUTO_ANIMATE_TITLE = 2;
public static final int AUTO_ANIMATE_CHILDREN = 4;
public static final int AUTO_ANIMATE_ALL = AUTO_ANIMATE_SCRIM | AUTO_ANIMATE_TITLE | AUTO_ANIMATE_CHILDREN;
public static final int AUTO_ANIMATE_DEFAULT = AUTO_ANIMATE_SCRIM;

private boolean mRefreshToolbar = true;
private int mToolbarId;
private Toolbar mToolbar;
Expand Down Expand Up @@ -135,6 +141,8 @@ public class CollapsingToolbarLayout extends FrameLayout {

WindowInsetsCompat mLastInsets;

private int mAutoAnimate = AUTO_ANIMATE_DEFAULT;

public CollapsingToolbarLayout(Context context) {
this(context, null);
}
Expand Down Expand Up @@ -236,6 +244,9 @@ public WindowInsetsCompat onApplyWindowInsets(View v,
// BEGIN MODIFICATION: set the value of maxNumberOfLines attribute to the mCollapsingTextHelper
TypedArray typedArray = context.obtainStyledAttributes(attrs, net.opacapp.multilinecollapsingtoolbar.R.styleable.CollapsingToolbarLayoutExtension, defStyleAttr, 0);
mCollapsingTextHelper.setMaxLines(typedArray.getInteger(net.opacapp.multilinecollapsingtoolbar.R.styleable.CollapsingToolbarLayoutExtension_maxLines, 3));
// load auto animation setting
mAutoAnimate = typedArray.getInt(net.opacapp.multilinecollapsingtoolbar.R.styleable.CollapsingToolbarLayoutExtension_autoAnimate, AUTO_ANIMATE_DEFAULT);
typedArray.recycle();
// END MODIFICATION
}

Expand Down Expand Up @@ -633,7 +644,14 @@ private void animateScrim(int targetAlpha) {
mScrimAnimator.addUpdateListener(new ValueAnimatorCompat.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimatorCompat animator) {
setScrimAlpha(animator.getAnimatedIntValue());
// BEGIN MODIFICATION by dmfs: apply scrim & blending automatically if configured that way
if ((mAutoAnimate & AUTO_ANIMATE_SCRIM) != 0) {
setScrimAlpha(animator.getAnimatedIntValue());
}
if ((mAutoAnimate & AUTO_ANIMATE_TITLE) != 0) {
mCollapsingTextHelper.setBlendRatio(animator.getAnimatedIntValue()/255f);
}
// END MODIFICATION by dmfs
}
});
} else if (mScrimAnimator.isRunning()) {
Expand Down Expand Up @@ -1303,18 +1321,31 @@ public void onOffsetChanged(AppBarLayout layout, int verticalOffset) {
}
}

// Show or hide the scrims if needed
updateScrimVisibility();
// BEGIN MODIFICATION by dmfs: apply scrim & blending gradually if configured that way
final float expandRatio = Math.abs(verticalOffset) /
(float) (getHeight() - ViewCompat.getMinimumHeight(CollapsingToolbarLayout.this) - insetTop);

if ((mAutoAnimate & AUTO_ANIMATE_SCRIM) == 0) {
// title blend it not auto-animated
setScrimAlpha((int) (expandRatio * 0xff));
}
else {
// Show or hide the scrims if needed
updateScrimVisibility();
}

if (mStatusBarScrim != null && insetTop > 0) {
ViewCompat.postInvalidateOnAnimation(CollapsingToolbarLayout.this);
}

// Update the collapsing text's fraction
final int expandRange = getHeight() - ViewCompat.getMinimumHeight(
CollapsingToolbarLayout.this) - insetTop;
mCollapsingTextHelper.setExpansionFraction(
Math.abs(verticalOffset) / (float) expandRange);
mCollapsingTextHelper.setExpansionFraction(expandRatio);

if ((mAutoAnimate & AUTO_ANIMATE_TITLE) == 0) {
// title blend it not auto-animated
mCollapsingTextHelper.setBlendRatio(expandRatio);
}
// END MODIFICATION
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
<resources>
<declare-styleable name="CollapsingToolbarLayoutExtension">
<attr name="maxLines" format="integer" />
<attr name="autoAnimate">
<flag name="none" value="0" />
<flag name="scrim" value="1" />
<flag name="title" value="2" />
<flag name="children" value="4" />
<flag name="all" value="7" />
</attr>
</declare-styleable>
</resources>
<!--END MODIFICATION-->

0 comments on commit 7f55603

Please sign in to comment.