Skip to content

Commit

Permalink
support ripple at api 21
Browse files Browse the repository at this point in the history
  • Loading branch information
mthli committed Jan 2, 2016
1 parent 24c83d7 commit a81e5e0
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 14 deletions.
3 changes: 2 additions & 1 deletion app/src/main/res/layout/item_bottom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp">
android:layout_marginEnd="16dp"
android:clickable="true">
</FrameLayout>

</FrameLayout>
3 changes: 2 additions & 1 deletion app/src/main/res/layout/item_center.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp">
android:layout_marginEnd="16dp"
android:clickable="true">
</FrameLayout>

</FrameLayout>
3 changes: 2 additions & 1 deletion app/src/main/res/layout/item_top.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp">
android:layout_marginEnd="16dp"
android:clickable="true">
</FrameLayout>

</FrameLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private RectF buildLeftBottomRect() {
return rectF;
}

private Path buildConvexPath() {
protected Path buildConvexPath() {
Path path = new Path();

path.moveTo(mBoundsF.left, (mBoundsF.top + mBoundsF.bottom) / 2.0f);
Expand Down Expand Up @@ -144,15 +144,15 @@ private Path buildConvexPath() {
return path;
}

protected int getColor() {
return mPaint.getColor();
}

@Override
public void setRadius(float radius) {
super.setRadius(radius);
}

public int getColor() {
return mPaint.getColor();
}

@Override
public void setColor(int color) {
super.setColor(color);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package io.github.mthli.slice;

import android.annotation.TargetApi;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Outline;
Expand All @@ -25,6 +26,7 @@
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Build;

import static io.github.mthli.slice.RoundRectDrawableWithShadow.calculateVerticalPadding;
import static io.github.mthli.slice.RoundRectDrawableWithShadow.calculateHorizontalPadding;
Expand All @@ -35,6 +37,7 @@
* <p>
* Simpler and uses less resources compared to GradientDrawable or ShapeDrawable.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
class RoundRectDrawable extends Drawable {
protected float mRadius;
protected final Paint mPaint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package io.github.mthli.slice;

import android.annotation.TargetApi;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
Expand All @@ -29,10 +30,12 @@
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.os.Build;

/**
* A rounded rectangle drawable which also includes a shadow around.
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
class RoundRectDrawableWithShadow extends Drawable {
// used to calculate content padding
final static double COS_45 = Math.cos(Math.toRadians(45));
Expand Down
59 changes: 53 additions & 6 deletions slice/src/main/java/io/github/mthli/slice/Slice.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@

package io.github.mthli.slice;

import android.annotation.TargetApi;
import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.Shape;
import android.os.Build;
import android.util.Log;
import android.view.View;
Expand All @@ -28,6 +35,7 @@ public class Slice {

public static final float DEFAULT_RADIUS_DP = 2.0f;
public static final float DEFAULT_ELEVATION_DP = 2.0f;
public static final int DEFAULT_RIPPLE_COLOR = 0x40000000;
public static final int DEFAULT_BACKGROUND_COLOR = 0xFFFAFAFA;

private View view;
Expand All @@ -38,6 +46,7 @@ public Slice(View view) {
init();
}

@SuppressWarnings("NewApi")
private void init() {
if (SDK_LOLLIPOP) {
drawable = new CustomRoundRectDrawable(DEFAULT_BACKGROUND_COLOR, dp2px(DEFAULT_RADIUS_DP));
Expand All @@ -51,34 +60,72 @@ private void init() {
view.setBackgroundDrawable(drawable);
}

setRipple(DEFAULT_RIPPLE_COLOR);
setElevation(DEFAULT_ELEVATION_DP);
}

private float dp2px(float dp) {
return view.getResources().getDisplayMetrics().density * dp;
}

public void setRadius(float radiusDp) {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private ColorStateList buildColorStateList(int normal, int pressed) {
return new ColorStateList(new int[][]{
new int[] {android.R.attr.state_pressed},
new int[] {android.R.attr.state_focused},
new int[] {android.R.attr.state_activated},
new int[] {}},
new int[] {pressed, pressed, pressed, normal}
);
}

public void setColor(int color) {
if (SDK_LOLLIPOP) {
((CustomRoundRectDrawable) drawable).setRadius(dp2px(radiusDp));
((CustomRoundRectDrawable) drawable).setColor(color);
} else {
((CustomRoundRectDrawableWithShadow) drawable).setRadius(dp2px(radiusDp));
((CustomRoundRectDrawableWithShadow) drawable).setColor(color);
}
}

@SuppressWarnings("NewApi")
public void setElevation(float elevationDp) {
if (SDK_LOLLIPOP) {
view.setElevation(dp2px(elevationDp));
} else {
Log.i(TAG, "setElevation() only support range from 0dp to 2dp pre API 21.");
((CustomRoundRectDrawableWithShadow) drawable).setShadowSize(dp2px(elevationDp));
}
}

public void setColor(int color) {
public void setRadius(float radiusDp) {
if (SDK_LOLLIPOP) {
((CustomRoundRectDrawable) drawable).setColor(color);
((CustomRoundRectDrawable) drawable).setRadius(dp2px(radiusDp));
} else {
((CustomRoundRectDrawableWithShadow) drawable).setColor(color);
((CustomRoundRectDrawableWithShadow) drawable).setRadius(dp2px(radiusDp));
}
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void setRipple(final int mask) {
if (SDK_LOLLIPOP) {
if (mask != 0) {
ShapeDrawable shape = new ShapeDrawable(new Shape() {
@Override
public void draw(Canvas canvas, Paint paint) {
paint.setColor(mask);
canvas.drawPath(((CustomRoundRectDrawable) drawable).buildConvexPath(), paint);
}
});

int color = ((CustomRoundRectDrawable) drawable).getColor();
RippleDrawable ripple = new RippleDrawable(buildColorStateList(color, mask), drawable, shape);

view.setBackground(ripple);
} else {
view.setBackground(drawable);
}
} else {
Log.i(TAG, "setRipple() only work for API 21+");
}
}

Expand Down

0 comments on commit a81e5e0

Please sign in to comment.