Last active
March 4, 2019 06:07
-
-
Save samuel22gj/d487905f6203ee724d32aada2dbad4c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ToolbarOffsetChangedListener implements AppBarLayout.OnOffsetChangedListener { | |
private static final String TAG = "ToolbarOffsetChangedListener"; | |
private Toolbar toolbar; | |
private Drawable[] icons; | |
/** | |
* 觸發開始改變顏色的 offset [0, - AppBar's height] | |
*/ | |
private int verticalOffsetTrigger = -1; | |
/** | |
* 改變顏色的作用距離 | |
*/ | |
private int transformDistance = 100; | |
private int backgroundExpandColor = Color.TRANSPARENT; | |
private int backgroundCollapseColor = Color.WHITE; | |
private int textExpandColor = Color.TRANSPARENT; | |
private int textCollapseColor = Color.BLACK; | |
private int iconExpandColor = Color.WHITE; | |
private int iconCollapseColor = Color.BLACK; | |
private int[] backgroundGradient; | |
private int[] textGradient; | |
private int[] iconGradient; | |
public ToolbarOffsetChangedListener(@NonNull Toolbar toolbar, @Nullable Drawable... icons) { | |
this.toolbar = toolbar; | |
this.icons = icons; | |
// Calculate default gradient | |
backgroundGradient = ColorUtils.getGradientARGB(backgroundExpandColor, backgroundCollapseColor, transformDistance + 1); | |
textGradient = ColorUtils.getGradientARGB(textExpandColor, textCollapseColor, transformDistance + 1); | |
iconGradient = ColorUtils.getGradientARGB(iconExpandColor, iconCollapseColor, transformDistance + 1); | |
} | |
public void setVerticalOffsetTrigger(int verticalOffsetTrigger) { | |
this.verticalOffsetTrigger = verticalOffsetTrigger; | |
} | |
public void setTransformDistance(int transformDistance) { | |
this.transformDistance = transformDistance; | |
} | |
public void setBackgroundColor(@ColorInt int expand, @ColorInt int collapse) { | |
backgroundExpandColor = expand; | |
backgroundCollapseColor = collapse; | |
backgroundGradient = ColorUtils.getGradientARGB(backgroundExpandColor, backgroundCollapseColor, transformDistance + 1); | |
} | |
public void setTextColor(@ColorInt int expand, @ColorInt int collapse) { | |
textExpandColor = expand; | |
textCollapseColor = collapse; | |
textGradient = ColorUtils.getGradientARGB(textExpandColor, textCollapseColor, transformDistance + 1); | |
} | |
public void setIconColor(@ColorInt int expand, @ColorInt int collapse) { | |
iconExpandColor = expand; | |
iconCollapseColor = collapse; | |
iconGradient = ColorUtils.getGradientARGB(iconExpandColor, iconCollapseColor, transformDistance + 1); | |
} | |
@Override | |
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { | |
int backgroundColor; | |
int textColor; | |
int iconColor; | |
if (verticalOffset > verticalOffsetTrigger) { | |
// Full expand | |
backgroundColor = backgroundGradient[0]; | |
textColor = textGradient[0]; | |
iconColor = iconGradient[0]; | |
} else if (verticalOffset < verticalOffsetTrigger - transformDistance) { | |
// Full collapse | |
backgroundColor = backgroundGradient[transformDistance]; | |
textColor = textGradient[transformDistance]; | |
iconColor = iconGradient[transformDistance]; | |
} else { | |
// During transform | |
int step = Math.abs(verticalOffset - verticalOffsetTrigger); | |
backgroundColor = backgroundGradient[step]; | |
textColor = textGradient[step]; | |
iconColor = iconGradient[step]; | |
} | |
toolbar.setBackgroundColor(backgroundColor); | |
toolbar.setTitleTextColor(textColor); | |
for (Drawable icon : icons) { | |
icon.mutate(); | |
icon.setColorFilter(colorColor, PorterDuff.Mode.SRC_ATOP); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment