Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions library/src/main/java/me/imid/swipebacklayout/lib/SwipeBackLayout.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -41,10 +42,12 @@ public class SwipeBackLayout extends FrameLayout {
*/
public static final int EDGE_BOTTOM = ViewDragHelper.EDGE_BOTTOM;

public static final int EDGE_TOP = ViewDragHelper.EDGE_TOP;

/**
* Edge flag set indicating all edges should be affected.
*/
public static final int EDGE_ALL = EDGE_LEFT | EDGE_RIGHT | EDGE_BOTTOM;
public static final int EDGE_ALL = EDGE_LEFT | EDGE_RIGHT | EDGE_BOTTOM | EDGE_TOP;

/**
* A view is not currently being dragged or animating as a result of a
Expand Down Expand Up @@ -72,7 +75,7 @@ public class SwipeBackLayout extends FrameLayout {
private static final int OVERSCROLL_DISTANCE = 10;

private static final int[] EDGE_FLAGS = {
EDGE_LEFT, EDGE_RIGHT, EDGE_BOTTOM, EDGE_ALL
EDGE_LEFT, EDGE_RIGHT, EDGE_BOTTOM, EDGE_TOP, EDGE_ALL
};

private int mEdgeFlag;
Expand Down Expand Up @@ -108,6 +111,8 @@ public class SwipeBackLayout extends FrameLayout {

private Drawable mShadowBottom;

private Drawable mShadowTop;

private float mScrimOpacity;

private int mScrimColor = DEFAULT_SCRIM_COLOR;
Expand Down Expand Up @@ -148,9 +153,12 @@ public SwipeBackLayout(Context context, AttributeSet attrs, int defStyle) {
R.drawable.shadow_right);
int shadowBottom = a.getResourceId(R.styleable.SwipeBackLayout_shadow_bottom,
R.drawable.shadow_bottom);
int shadowTop = a.getResourceId(R.styleable.SwipeBackLayout_shadow_top,
R.drawable.shadow_top);
setShadow(shadowLeft, EDGE_LEFT);
setShadow(shadowRight, EDGE_RIGHT);
setShadow(shadowBottom, EDGE_BOTTOM);
setShadow(shadowTop, EDGE_TOP);
a.recycle();
final float density = getResources().getDisplayMetrics().density;
final float minVel = MIN_FLING_VELOCITY * density;
Expand Down Expand Up @@ -278,6 +286,7 @@ public static interface SwipeListener {
* @see #EDGE_LEFT
* @see #EDGE_RIGHT
* @see #EDGE_BOTTOM
* @see #EDGE_TOP
*/
public void onEdgeTouch(int edgeFlag);

Expand Down Expand Up @@ -308,6 +317,7 @@ public void setScrollThresHold(float threshold) {
* @see #EDGE_LEFT
* @see #EDGE_RIGHT
* @see #EDGE_BOTTOM
* @see #EDGE_TOP
*/
public void setShadow(Drawable shadow, int edgeFlag) {
if ((edgeFlag & EDGE_LEFT) != 0) {
Expand All @@ -316,6 +326,8 @@ public void setShadow(Drawable shadow, int edgeFlag) {
mShadowRight = shadow;
} else if ((edgeFlag & EDGE_BOTTOM) != 0) {
mShadowBottom = shadow;
} else if ((edgeFlag & EDGE_TOP) != 0) {
mShadowTop = shadow;
}
invalidate();
}
Expand All @@ -328,6 +340,7 @@ public void setShadow(Drawable shadow, int edgeFlag) {
* @see #EDGE_LEFT
* @see #EDGE_RIGHT
* @see #EDGE_BOTTOM
* @see #EDGE_TOP
*/
public void setShadow(int resId, int edgeFlag) {
setShadow(getResources().getDrawable(resId), edgeFlag);
Expand All @@ -350,6 +363,9 @@ public void scrollToFinishActivity() {
} else if ((mEdgeFlag & EDGE_BOTTOM) != 0) {
top = -childHeight - mShadowBottom.getIntrinsicHeight() - OVERSCROLL_DISTANCE;
mTrackingEdge = EDGE_BOTTOM;
} else if ((mEdgeFlag & EDGE_TOP) != 0) {
top = childWidth + mShadowLeft.getIntrinsicWidth() + OVERSCROLL_DISTANCE;
mTrackingEdge = EDGE_TOP;
}

mDragHelper.smoothSlideViewTo(mContentView, left, top);
Expand Down Expand Up @@ -420,6 +436,8 @@ private void drawScrim(Canvas canvas, View child) {
canvas.clipRect(child.getRight(), 0, getRight(), getHeight());
} else if ((mTrackingEdge & EDGE_BOTTOM) != 0) {
canvas.clipRect(child.getLeft(), child.getBottom(), getRight(), getHeight());
} else if ((mTrackingEdge & EDGE_TOP) != 0) {
canvas.clipRect(child.getLeft(), 0, getRight(), child.getTop() + getStatusBarHeight());
}
canvas.drawColor(color);
}
Expand Down Expand Up @@ -448,6 +466,22 @@ private void drawShadow(Canvas canvas, View child) {
mShadowBottom.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
mShadowBottom.draw(canvas);
}

if ((mEdgeFlag & EDGE_TOP) != 0) {
mShadowTop.setBounds(childRect.left, childRect.top - mShadowTop.getIntrinsicHeight(), childRect.right,
childRect.top + getStatusBarHeight());
mShadowTop.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
mShadowTop.draw(canvas);
}
}

private int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}

public void attachToActivity(Activity activity) {
Expand Down Expand Up @@ -488,6 +522,8 @@ public boolean tryCaptureView(View view, int i) {
mTrackingEdge = EDGE_RIGHT;
} else if (mDragHelper.isEdgeTouched(EDGE_BOTTOM, i)) {
mTrackingEdge = EDGE_BOTTOM;
} else if (mDragHelper.isEdgeTouched(EDGE_TOP, i)) {
mTrackingEdge = EDGE_TOP;
}
if (mListeners != null && !mListeners.isEmpty()) {
for (SwipeListener listener : mListeners) {
Expand All @@ -506,7 +542,7 @@ public int getViewHorizontalDragRange(View child) {

@Override
public int getViewVerticalDragRange(View child) {
return mEdgeFlag & EDGE_BOTTOM;
return mEdgeFlag & (EDGE_BOTTOM | EDGE_TOP);
}

@Override
Expand All @@ -521,6 +557,9 @@ public void onViewPositionChanged(View changedView, int left, int top, int dx, i
} else if ((mTrackingEdge & EDGE_BOTTOM) != 0) {
mScrollPercent = Math.abs((float) top
/ (mContentView.getHeight() + mShadowBottom.getIntrinsicHeight()));
} else if ((mTrackingEdge & EDGE_TOP) != 0) {
mScrollPercent = Math.abs((float) top
/ (mContentView.getHeight() + mShadowTop.getIntrinsicHeight()));
}
mContentLeft = left;
mContentTop = top;
Expand Down Expand Up @@ -558,6 +597,9 @@ public void onViewReleased(View releasedChild, float xvel, float yvel) {
} else if ((mTrackingEdge & EDGE_BOTTOM) != 0) {
top = yvel < 0 || yvel == 0 && mScrollPercent > mScrollThreshold ? -(childHeight
+ mShadowBottom.getIntrinsicHeight() + OVERSCROLL_DISTANCE) : 0;
} else if ((mTrackingEdge & EDGE_TOP) != 0) {
top = yvel > 0 || yvel == 0 && mScrollPercent > mScrollThreshold ? childHeight
+ mShadowBottom.getIntrinsicHeight() + OVERSCROLL_DISTANCE : 0;
}

mDragHelper.settleCapturedViewAt(left, top);
Expand All @@ -580,6 +622,8 @@ public int clampViewPositionVertical(View child, int top, int dy) {
int ret = 0;
if ((mTrackingEdge & EDGE_BOTTOM) != 0) {
ret = Math.min(0, Math.max(top, -child.getHeight()));
} else if ((mTrackingEdge & EDGE_TOP) != 0) {
ret = Math.min(child.getHeight(), Math.max(top, 0));
}
return ret;
}
Expand Down
Binary file added library/src/main/res/drawable-xhdpi/shadow_top.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion library/src/main/res/values/attrs.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
<enum name="left" value="0" />
<enum name="right" value="1" />
<enum name="bottom" value="2" />
<enum name="all" value="3" />
<enum name="top" value="3" />
<enum name="all" value="4" />
</attr>
<attr name="shadow_left" format="reference"/>
<attr name="shadow_right" format="reference"/>
<attr name="shadow_bottom" format="reference"/>
<attr name="shadow_top" format="reference"/>
</declare-styleable>

<attr name="SwipeBackLayoutStyle" format="reference"/>
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/styles.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<item name="shadow_left">@drawable/shadow_left</item>
<item name="shadow_right">@drawable/shadow_right</item>
<item name="shadow_bottom">@drawable/shadow_bottom</item>
<item name="shadow_top">@drawable/shadow_top</item>
</style>

</resources>