Skip to content

Instantly share code, notes, and snippets.

@bholota
Created November 15, 2015 19:28
Show Gist options
  • Save bholota/2e14f5ce43a343422c22 to your computer and use it in GitHub Desktop.
Save bholota/2e14f5ce43a343422c22 to your computer and use it in GitHub Desktop.
public static class FadeoutBehavior extends CoordinatorLayout.Behavior<View> {
private int startPos = 0;
private int finalPos = 0;
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency instanceof AppBarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
shouldInitProperties(child, dependency);
float fullDistance = startPos - finalPos;
float currDistance = child.getY() + child.getHeight() / 2f - finalPos;
float x = currDistance * 100 / fullDistance;
child.setAlpha(x / 100f);
return true;
}
private void shouldInitProperties(View child, View dependency) {
if(startPos == 0) {
startPos = (int) child.getY() + child.getHeight() / 2;
}
if(finalPos == 0 ){
finalPos = dependency.getHeight() / 2;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment