Skip to content

Commit

Permalink
Allow customizing how the content of BottomSheet is dimmed
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
Emil Sjolander committed Jun 8, 2015
1 parent 18b989e commit b71a338
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.flipboard.bottomsheet;

import android.view.View;

/**
* A very simple view transformer which only handles applying a correct dim.
*/
public abstract class BaseViewTransformer implements ViewTransformer {

public static final float MAX_DIM_ALPHA = 0.7f;

@Override
public float getDimAlpha(float translation, float maxTranslation, float peekedTranslation, BottomSheetLayout parent, View view) {
float progress = translation / maxTranslation;
return progress * MAX_DIM_ALPHA;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ public void onAnimationCancel(Animator animation) {

}

private class IdentityViewTransformer extends BaseViewTransformer {
@Override
public void transformView(float translation, float maxTranslation, float peekedTranslation, BottomSheetLayout parent, View view) {
// no-op
}
}

public enum State {
HIDDEN,
PEEKED,
Expand All @@ -59,7 +66,6 @@ public interface OnSheetStateChangeListener {
void onSheetStateChanged(State state);
}

private static final float MAX_DIM_ALPHA = 0.7f;
private static final long ANIMATION_DURATION = 300;

private Rect contentClipRect = new Rect();
Expand All @@ -71,7 +77,7 @@ public interface OnSheetStateChangeListener {
private VelocityTracker velocityTracker;
private float minFlingVelocity;
private float touchSlop;
private ViewTransformer defaultViewTransformer;
private ViewTransformer defaultViewTransformer = new IdentityViewTransformer();
private ViewTransformer viewTransformer;
private OnSheetDismissedListener onSheetDismissedListener;
private boolean shouldDimContentView = true;
Expand Down Expand Up @@ -178,9 +184,7 @@ private void setSheetTranslation(float sheetTranslation) {
this.contentClipRect.set(0, 0, getWidth(), bottomClip);
getSheetView().setTranslationY(getHeight() - sheetTranslation);
transformView(sheetTranslation);

float progress = Math.max(sheetTranslation - getPeekSheetTranslation(), 0) / (getHeight() - getPeekSheetTranslation());
dimView.setAlpha(shouldDimContentView ? progress * MAX_DIM_ALPHA : 0);
dimView.setAlpha(shouldDimContentView ? getDimAlpha(sheetTranslation) : 0);
}

private void transformView(float sheetTranslation) {
Expand All @@ -191,6 +195,15 @@ private void transformView(float sheetTranslation) {
}
}

private float getDimAlpha(float sheetTranslation) {
if (viewTransformer != null) {
return viewTransformer.getDimAlpha(sheetTranslation, getMaxSheetTranslation(), getPeekSheetTranslation(), this, getContentView());
} else if (defaultViewTransformer != null) {
return defaultViewTransformer.getDimAlpha(sheetTranslation, getMaxSheetTranslation(), getPeekSheetTranslation(), this, getContentView());
}
return 0;
}

private float getSheetTranslation() {
return sheetTranslation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,18 @@ public interface ViewTransformer {
*/
void transformView(float translation, float maxTranslation, float peekedTranslation, BottomSheetLayout parent, View view);

/**
* Called on when the translation of the sheet view changes allowing you to customize the amount of dimming which
* is applied to the content view.
*
* @param translation The current translation of the presented sheet view.
* @param maxTranslation The max translation of the presented sheet view.
* @param peekedTranslation The peeked state translation of the presented sheet view.
* @param parent The BottomSheet presenting the sheet view.
* @param view The content view to transform.
*
* @return The alpha to apply to the dim overlay.
*/
float getDimAlpha(float translation, float maxTranslation, float peekedTranslation, BottomSheetLayout parent, View view);

}

0 comments on commit b71a338

Please sign in to comment.