Skip to content

Commit

Permalink
Added rounded corner support #7 #16
Browse files Browse the repository at this point in the history
- Note: software rendering layer is used for API 17 and below, use with caution
  • Loading branch information
balysv committed Jan 31, 2015
1 parent ebc2c1a commit ce4be50
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ app:rippleFadeDuration="75" // duration of fade out effect on ripple
app:rippleDelayClick="true" // if true, delays calls to OnClickListeners until ripple effect ends
app:rippleBackground="#FFFFFF" // background under ripple drawable; used with rippleOverlay="false"
app:ripplePersistent="true" // if true, ripple background color persists after animation, until setRadius(0) is called
app:rippleRoundedCorners="10dp" // radius of corners of ripples. Note: it uses software rendering pipeline for API 17 and below
```

Set an `OnClickListener` to `MaterialRippleLayout`:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Property;
import android.util.TypedValue;
Expand All @@ -50,17 +53,18 @@

public class MaterialRippleLayout extends FrameLayout {

private static final int DEFAULT_DURATION = 350;
private static final int DEFAULT_FADE_DURATION = 75;
private static final float DEFAULT_DIAMETER_DP = 35;
private static final float DEFAULT_ALPHA = 0.2f;
private static final int DEFAULT_COLOR = Color.BLACK;
private static final int DEFAULT_BACKGROUND = Color.TRANSPARENT;
private static final boolean DEFAULT_HOVER = true;
private static final boolean DEFAULT_DELAY_CLICK = true;
private static final boolean DEFAULT_PERSISTENT = false;
private static final boolean DEFAULT_SEARCH_ADAPTER = false;
private static final boolean DEFAULT_RIPPLE_OVERLAY = false;
private static final int DEFAULT_DURATION = 350;
private static final int DEFAULT_FADE_DURATION = 75;
private static final float DEFAULT_DIAMETER_DP = 35;
private static final float DEFAULT_ALPHA = 0.2f;
private static final int DEFAULT_COLOR = Color.BLACK;
private static final int DEFAULT_BACKGROUND = Color.TRANSPARENT;
private static final boolean DEFAULT_HOVER = true;
private static final boolean DEFAULT_DELAY_CLICK = true;
private static final boolean DEFAULT_PERSISTENT = false;
private static final boolean DEFAULT_SEARCH_ADAPTER = false;
private static final boolean DEFAULT_RIPPLE_OVERLAY = false;
private static final int DEFAULT_ROUNDED_CORNERS = 0;

private static final int FADE_EXTRA_DELAY = 50;
private static final long HOVER_DURATION = 2500;
Expand All @@ -79,6 +83,7 @@ public class MaterialRippleLayout extends FrameLayout {
private boolean ripplePersistent;
private Drawable rippleBackground;
private boolean rippleInAdapter;
private float rippleRoundedCorners;

private float radius;

Expand All @@ -91,6 +96,8 @@ public class MaterialRippleLayout extends FrameLayout {
private Point currentCoords = new Point();
private Point previousCoords = new Point();

private int layerType;

private boolean eventCancelled;
private boolean prepressed;
private int positionInAdapter;
Expand Down Expand Up @@ -132,11 +139,14 @@ public MaterialRippleLayout(Context context, AttributeSet attrs, int defStyle) {
rippleBackground = new ColorDrawable(a.getColor(R.styleable.MaterialRippleLayout_rippleBackground, DEFAULT_BACKGROUND));
ripplePersistent = a.getBoolean(R.styleable.MaterialRippleLayout_ripplePersistent, DEFAULT_PERSISTENT);
rippleInAdapter = a.getBoolean(R.styleable.MaterialRippleLayout_rippleInAdapter, DEFAULT_SEARCH_ADAPTER);
rippleRoundedCorners = a.getDimensionPixelSize(R.styleable.MaterialRippleLayout_rippleRoundedCorners, DEFAULT_ROUNDED_CORNERS);

a.recycle();

paint.setColor(rippleColor);
paint.setAlpha(rippleAlpha);

enableClipPathSupportIfNecessary();
}


Expand Down Expand Up @@ -272,13 +282,14 @@ private void cancelPressedEvent() {
private SimpleOnGestureListener longClickListener = new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) {
mHasPerformedLongPress = childView.performLongClick();
if(mHasPerformedLongPress){
if (mHasPerformedLongPress) {
if (rippleHover) {
startRipple(null);
}
cancelPressedEvent();
}
}

@Override
public boolean onDown(MotionEvent e) {
mHasPerformedLongPress = false;
Expand Down Expand Up @@ -439,6 +450,12 @@ public void draw(Canvas canvas) {
}
super.draw(canvas);
if (!positionChanged) {
if (rippleRoundedCorners != 0) {
Path clipPath = new Path();
RectF rect = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
clipPath.addRoundRect(rect, rippleRoundedCorners, rippleRoundedCorners, Path.Direction.CW);
canvas.clipPath(clipPath);
}
canvas.drawCircle(currentCoords.x, currentCoords.y, radius, paint);
}
} else {
Expand Down Expand Up @@ -546,19 +563,41 @@ public void setRippleInAdapter(boolean rippleInAdapter) {
this.rippleInAdapter = rippleInAdapter;
}

public void setRippleRoundedCorners(int rippleRoundedCorner) {
this.rippleRoundedCorners = rippleRoundedCorner;
enableClipPathSupportIfNecessary();
}

public void setDefaultRippleAlpha(int alpha) {
this.rippleAlpha = alpha;
paint.setAlpha(alpha);
invalidate();
}

/**
* {@link Canvas#clipPath(Path)} is not supported in hardware accelerated layers
* before API 18. Use software layer instead
* <p/>
* https://developer.android.com/guide/topics/graphics/hardware-accel.html#unsupported
*/
private void enableClipPathSupportIfNecessary() {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (rippleRoundedCorners != 0) {
layerType = getLayerType();
setLayerType(LAYER_TYPE_SOFTWARE, null);
} else {
setLayerType(layerType, null);
}
}
}

/*
* Helper
*/
private class PerformClickEvent implements Runnable {

@Override public void run() {
if(mHasPerformedLongPress) return;
if (mHasPerformedLongPress) return;

// if parent is an AdapterView, try to call its ItemClickListener
if (getParent() instanceof AdapterView) {
Expand Down Expand Up @@ -627,6 +666,7 @@ public static class RippleBuilder {
private boolean ripplePersistent = DEFAULT_PERSISTENT;
private int rippleBackground = DEFAULT_BACKGROUND;
private boolean rippleSearchAdapter = DEFAULT_SEARCH_ADAPTER;
private float rippleRoundedCorner = DEFAULT_ROUNDED_CORNERS;

public RippleBuilder(View child) {
this.child = child;
Expand Down Expand Up @@ -688,6 +728,11 @@ public RippleBuilder rippleInAdapter(boolean inAdapter) {
return this;
}

public RippleBuilder rippleRoundedCorners(int radiusDp) {
this.rippleRoundedCorner = radiusDp;
return this;
}

public MaterialRippleLayout create() {
MaterialRippleLayout layout = new MaterialRippleLayout(context);
layout.setRippleColor(rippleColor);
Expand All @@ -701,6 +746,7 @@ public MaterialRippleLayout create() {
layout.setRippleOverlay(rippleOverlay);
layout.setRippleBackground(rippleBackground);
layout.setRippleInAdapter(rippleSearchAdapter);
layout.setRippleRoundedCorners((int) dpToPx(context.getResources(), rippleRoundedCorner));

ViewGroup.LayoutParams params = child.getLayoutParams();
ViewGroup parent = (ViewGroup) child.getParent();
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attributes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
<attr name="rippleDelayClick" format="boolean" localization="suggested" />
<attr name="ripplePersistent" format="boolean" localization="suggested" />
<attr name="rippleInAdapter" format="boolean" localization="suggested" />
<attr name="rippleRoundedCorners" format="dimension" localization="suggested" />
</declare-styleable>
</resources>

0 comments on commit ce4be50

Please sign in to comment.