Skip to content

Commit

Permalink
- Removed unused imports
Browse files Browse the repository at this point in the history
- Fix bug with ripple backgrounds not finishing animation
- Cancel timers on pop
  • Loading branch information
shalskar committed Feb 3, 2016
1 parent 5a012c6 commit 9211e4f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 24 deletions.
1 change: 0 additions & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.util.Log;
import android.view.View;

public class BlurBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public class PeekAndPop {
public static final int FLING_UPWARDS = 0;
public static final int FLING_DOWNWARDS = 1;

protected static final int PEEK_VIEW_MARGIN = 40;
private static final int PEEK_VIEW_MARGIN = 40;

protected static final long LONG_CLICK_DURATION = 200;
protected static final long LONG_HOLD_DURATION = 850;
protected static final long HOLD_AND_RELEASE_DURATION = 100;

protected static final int FLING_VELOCITY_THRESHOLD = 3000;
private static final int FLING_VELOCITY_THRESHOLD = 3000;
private static final float FLING_VELOCITY_MAX = 1000;

protected static final int ANIMATION_PEEK_DURATION = 300;
Expand All @@ -53,10 +53,10 @@ public class PeekAndPop {
protected ViewGroup peekLayout;
protected PeekAnimationHelper peekAnimationHelper;

protected boolean blurBackground;
protected boolean animateFling;
protected boolean allowUpwardsFling;
protected boolean allowDownwardsFling;
private boolean blurBackground;
private boolean animateFling;
private boolean allowUpwardsFling;
private boolean allowDownwardsFling;
private int customLongHoldDuration = -1;

protected ArrayList<LongHoldView> longHoldViews;
Expand Down Expand Up @@ -118,9 +118,9 @@ protected void initialisePeekView() {

// Center onPeek view in the onPeek layout and add to the container view group
peekLayout = (RelativeLayout) inflater.inflate(R.layout.peek_background, contentView, false);

peekView = inflater.inflate(builder.peekLayoutId, peekLayout, false);
peekView.setId(R.id.peek_view);

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) peekView.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
Expand All @@ -129,19 +129,22 @@ protected void initialisePeekView() {

peekLayout.addView(peekView, layoutParams);
contentView.addView(peekLayout);

peekLayout.setVisibility(View.GONE);
peekLayout.setAlpha(0);
peekLayout.requestLayout();

peekAnimationHelper = new PeekAnimationHelper(builder.activity.getApplicationContext(), peekLayout, peekView);

peekLayout.requestLayout();
bringViewsToFront();
initialiseViewTreeObserver();
resetViews();
}

/**
* If lollipop or above, use elevation to bring peek views to the front
*/
private void bringViewsToFront() {
// If lollipop or above, use elevation to bring peek views to the front
if (Build.VERSION.SDK_INT >= 21) {
peekLayout.setElevation(10f);
peekView.setElevation(10f);
Expand Down Expand Up @@ -189,8 +192,7 @@ protected void initialiseGestureListener(@NonNull View view, int position) {
* If the user is within the bounds, and is at the edges of the view, then
* move it appropriately.
*/

private void handleTouch(@NonNull View view, @NonNull MotionEvent event, int position) {
protected void handleTouch(@NonNull View view, @NonNull MotionEvent event, int position) {
if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
pop(view, position);
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
Expand Down Expand Up @@ -278,7 +280,7 @@ private void initialisePeekViewOriginalPosition() {
* @param longClickView the view that was long clicked
* @param index the view that long clicked
*/
private void peek(@NonNull View longClickView, int index) {
protected void peek(@NonNull View longClickView, int index) {
if (onGeneralActionListener != null)
onGeneralActionListener.onPeek(longClickView, index);

Expand All @@ -301,6 +303,8 @@ else if (Build.VERSION.SDK_INT < 17 && blurBackground)

gestureListener.setView(longClickView);
gestureListener.setPosition(index);

PeekAnimationHelper.forceRippleAnimation(longClickView);
}

private void blurBackground() {
Expand All @@ -314,15 +318,14 @@ private void blurBackground() {
* @param longClickView the view that was long clicked
* @param index the view that long clicked
*/
private void pop(@NonNull View longClickView, int index) {
protected void pop(@NonNull View longClickView, int index) {
if (onGeneralActionListener != null)
onGeneralActionListener.onPop(longClickView, index);

if (currentHoldAndReleaseView != null && onHoldAndReleaseListener != null) {
if (currentHoldAndReleaseView != null && onHoldAndReleaseListener != null)
onHoldAndReleaseListener.onHoldAndRelease(currentHoldAndReleaseView.getView(), currentHoldAndReleaseView.getPosition());
currentHoldAndReleaseView.getHoldAndReleaseTimer().cancel();
currentHoldAndReleaseView = null;
}

resetTimers();

peekAnimationHelper.animatePop(new Animator.AnimatorListener() {
@Override
Expand Down Expand Up @@ -375,7 +378,19 @@ private void resetViews() {
peekView.setScaleY(0.85f);
}

public void destroy(){
private void resetTimers(){
currentHoldAndReleaseView = null;
for (HoldAndReleaseView holdAndReleaseView : holdAndReleaseViews) {
if (holdAndReleaseView.getHoldAndReleaseTimer() != null)
holdAndReleaseView.getHoldAndReleaseTimer().cancel();
}
for (LongHoldView longHoldView : longHoldViews) {
if (longHoldView.getLongHoldTimer() != null)
longHoldView.getLongHoldTimer().cancel();
}
}

public void destroy() {
if (currentHoldAndReleaseView != null && onHoldAndReleaseListener != null) {
currentHoldAndReleaseView.getHoldAndReleaseTimer().cancel();
currentHoldAndReleaseView = null;
Expand All @@ -392,7 +407,7 @@ public void destroy(){
builder = null;
}

public void setFlingTypes(boolean allowUpwardsFling, boolean allowDownwardsFling){
public void setFlingTypes(boolean allowUpwardsFling, boolean allowDownwardsFling) {
this.allowUpwardsFling = allowUpwardsFling;
this.allowDownwardsFling = allowDownwardsFling;
}
Expand Down Expand Up @@ -509,7 +524,7 @@ public Builder peekLayout(@LayoutRes int peekLayoutId) {
}

/**
* Views which will open the onPeek view when long clicked
* Views which will show the peek view when long clicked
*
* @param longClickViews One or more views to handle on long click events
* @return
Expand Down Expand Up @@ -605,7 +620,7 @@ public Builder animateFling(boolean animateFling) {
/**
* Set the accepted fling types, defaults to both being true.
*/
public Builder flingTypes(boolean allowUpwardsFling, boolean allowDownwardsFling){
public Builder flingTypes(boolean allowUpwardsFling, boolean allowDownwardsFling) {
this.allowUpwardsFling = allowUpwardsFling;
this.allowDownwardsFling = allowDownwardsFling;
return this;
Expand Down Expand Up @@ -643,6 +658,7 @@ public boolean onTouch(final View view, MotionEvent event) {
} else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
longHoldTimer.cancel();
}

if (peekShown)
handleTouch(view, event, position);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.Configuration;
import android.util.DisplayMetrics;
import android.graphics.drawable.RippleDrawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.OvershootInterpolator;
Expand Down Expand Up @@ -131,4 +133,16 @@ public void animateFling(float velocityX, float velocityY, int duration, long po
animatorTranslateX.start();
}
}

/**
* Workaround due to custom long touch listener causing ripples not to pop.
*
* @param view
*/
public static void forceRippleAnimation(@NonNull View view){
if(view.getBackground() instanceof RippleDrawable)
view.getBackground().setState(new int[0]);
if(Build.VERSION.SDK_INT >= 23 && view.getForeground() instanceof RippleDrawable)
view.getForeground().setState(new int[0]);
}
}

0 comments on commit 9211e4f

Please sign in to comment.