Last active
January 15, 2020 20:44
-
-
Save NikolaDespotoski/e587669f3d9b1519bc5e to your computer and use it in GitHub Desktop.
Toggle Toolbar background alpha and alpha of its title view.
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 ToolbarAlphaScrollBehavior extends CoordinatorLayout.Behavior<android.support.v7.widget.Toolbar> { | |
private ColorDrawable mStatusBarColorDrawable; | |
private int mStatusBarColor; | |
private TextView mTitleView; | |
private boolean searchedForTitleView = false; | |
public ToolbarAlphaScrollBehavior(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
mStatusBarColor = ContextCompat.getColor(context, R.color.primary_dark); | |
mStatusBarColor = getColorWithAlpha(0, mStatusBarColor); | |
mStatusBarColorDrawable = new ColorDrawable(mStatusBarColor); | |
} | |
public static int getColorWithAlpha(float alpha, int baseColor) { | |
int a = Math.min(255, Math.max(0, (int) (alpha * 255))) << 24; | |
int rgb = 0x00ffffff & baseColor; | |
return a + rgb; | |
} | |
public ToolbarAlphaScrollBehavior() { | |
} | |
@Override | |
public boolean layoutDependsOn(CoordinatorLayout parent, Toolbar child, View dependency) { | |
return dependency instanceof AppBarLayout; | |
} | |
@Override | |
public boolean onInterceptTouchEvent(CoordinatorLayout parent, Toolbar child, MotionEvent ev) { | |
return ev == null || super.onInterceptTouchEvent(parent, child, ev); | |
} | |
@Override | |
public boolean onDependentViewChanged(CoordinatorLayout parent, Toolbar child, View dependency) { | |
if (dependency instanceof AppBarLayout) { | |
float ratio = (float) getCurrentScrollValue(child, dependency) / getTotalScrollRange(child, dependency); | |
float alpha = 1f - Math.min(1f, Math.max(0f, ratio)); | |
int drawableAlpha = (int) (alpha * ViewCompatHelper.DRAWABLE_ALPHA); | |
// Log.i("ToolbarAlphaScrollBehavior", "Alpha: " + alpha); | |
if (ViewCompatHelper.isL()) { | |
child.getBackground().setAlpha(drawableAlpha); | |
} else if (ViewCompatHelper.isKitKat()) { | |
ViewGroup toolbarParent = (ViewGroup) child.getParent(); | |
if (toolbarParent.getChildCount() == 2) { | |
int count = toolbarParent.getChildCount(); | |
for (int i = count - 1; i >= 0; i--) { | |
toolbarParent.getChildAt(i).getBackground().setAlpha(drawableAlpha); | |
} | |
} | |
} else { | |
child.getBackground().setAlpha(drawableAlpha); | |
} | |
// setStatusBarColor(parent, drawableAlpha); | |
if (mTitleView != null) { | |
ViewCompat.setAlpha(mTitleView, alpha); | |
return false; | |
} | |
if (!searchedForTitleView) { | |
mTitleView = ViewHelper.getTitleView(child); | |
searchedForTitleView = true; | |
} | |
} | |
return false; | |
} | |
private void setStatusBarColor(CoordinatorLayout parent, int alpha) { | |
ColorDrawable statusBarBackground = (ColorDrawable) parent.getStatusBarBackground(); | |
statusBarBackground.setColor(getColorWithAlpha(alpha, statusBarBackground.getColor())); | |
parent.setStatusBarBackground(statusBarBackground); | |
} | |
private int getCurrentScrollValue(Toolbar child, View dependency) { | |
return dependency.getBottom() - child.getTop(); | |
} | |
private float getTotalScrollRange(Toolbar child, View dependency) { | |
return Math.max(dependency.getHeight(), ((AppBarLayout) dependency).getTotalScrollRange()) - child.getTop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This guy is the worst. Such a tease lol.. Let's do better, honestly.