Skip to content

Commit

Permalink
MaterialCardView add ripple drawable to foreground
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 208993686
  • Loading branch information
ymarian authored and wcshi committed Aug 17, 2018
1 parent 43bdfac commit 09673a5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ public void setLayoutParams(ViewGroup.LayoutParams params) {
}
}

@Override
public void setClickable(boolean clickable) {
super.setClickable(clickable);
cardViewHelper.updateForeground();
}

@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
contentLayout.addView(child, index, params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,40 @@
import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Outline;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.RippleDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Build.VERSION_CODES;
import android.support.annotation.ColorInt;
import android.support.annotation.Dimension;
import android.support.annotation.Nullable;
import android.support.annotation.RestrictTo;
import com.google.android.material.ripple.RippleUtils;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewOutlineProvider;
import java.util.Arrays;

/** @hide */
@RestrictTo(LIBRARY_GROUP)
class MaterialCardViewHelper {

private static final int DEFAULT_STROKE_VALUE = -1;

private final MaterialCardView materialCardView;

private int strokeColor;
private int strokeWidth;
private @ColorInt int strokeColor;
private @ColorInt int rippleColor;
private @Dimension int strokeWidth;

public MaterialCardViewHelper(MaterialCardView card) {
materialCardView = card;
Expand All @@ -51,7 +64,7 @@ public void loadFromAttributes(TypedArray attributes) {
strokeColor =
attributes.getColor(R.styleable.MaterialCardView_strokeColor, DEFAULT_STROKE_VALUE);
strokeWidth = attributes.getDimensionPixelSize(R.styleable.MaterialCardView_strokeWidth, 0);

rippleColor = getRippleColor();
updateForeground();
adjustContentPadding();
}
Expand All @@ -61,7 +74,6 @@ void setStrokeColor(@ColorInt int strokeColor) {
updateForeground();
}


@TargetApi(VERSION_CODES.LOLLIPOP)
void createOutlineProvider(@Nullable View contentView) {
if (contentView == null) {
Expand Down Expand Up @@ -116,7 +128,8 @@ void updateForeground() {
*/
private Drawable createForegroundDrawable() {
GradientDrawable fgDrawable = new GradientDrawable();
fgDrawable.setCornerRadius(materialCardView.getRadius());
float radius = materialCardView.getRadius();
fgDrawable.setCornerRadius(radius);

// In order to set a stroke, a size and color both need to be set. We default to a zero-width
// width size, but won't set a default color. This prevents drawing a stroke that blends in with
Expand All @@ -125,7 +138,28 @@ private Drawable createForegroundDrawable() {
fgDrawable.setStroke(strokeWidth, strokeColor);
}

return fgDrawable;
if (!materialCardView.isClickable()) {
return fgDrawable;
}

Drawable rippleDrawable;
if (RippleUtils.USE_FRAMEWORK_RIPPLE) {
//noinspection NewApi
rippleDrawable =
new RippleDrawable(
ColorStateList.valueOf(rippleColor), null, createForegroundShape(radius));
} else {
rippleDrawable = new StateListDrawable();
Drawable foregroundShape = createForegroundShape(radius);
DrawableCompat.setTint(foregroundShape, rippleColor);
((StateListDrawable) rippleDrawable)
.addState(new int[] {android.R.attr.state_pressed}, foregroundShape);
}

return new LayerDrawable(
new Drawable[] {
rippleDrawable, fgDrawable,
});
}

/** Guarantee at least enough content padding to account for the stroke width. */
Expand All @@ -137,4 +171,19 @@ private void adjustContentPadding() {
materialCardView.setContentPadding(
contentPaddingLeft, contentPaddingTop, contentPaddingRight, contentPaddingBottom);
}

private int getRippleColor() {
Context context = materialCardView.getContext();
TypedValue value = new TypedValue();
context.getTheme().resolveAttribute(R.attr.colorControlHighlight, value, true);
return value.data;
}

private Drawable createForegroundShape(float radius) {
float[] radii = new float[8];
Arrays.fill(radii, radius);
RoundRectShape shape = new RoundRectShape(radii, null, null);
ShapeDrawable shapeDrawable = new ShapeDrawable(shape);
return shapeDrawable;
}
}

7 comments on commit 09673a5

@danielesegato
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.0.0 is out, why hasn't this been merged in?

the new component still have no ripple effect?

@ymarian
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey you are correct.
We are releasing 1.1.0-alpha01 this week which will include this.

@jakobulbrich
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this fix also be released in the old support library which is currently on version 28.0.0?

@dsn5ft
Copy link
Contributor

@dsn5ft dsn5ft commented on 09673a5 Nov 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that the 1.x path is stable, fixes for the Design library (and any other androidx support library) will not be released for the 28.x path. 28.0.0 is the last version there.

@OleksandrGrument
Copy link

@OleksandrGrument OleksandrGrument commented on 09673a5 Nov 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that the 1.x path is stable, fixes for the Design library (and any other androidx support library) will not be released for the 28.x path. 28.0.0 is the last version there.

So what version of com.android.support:design:xxxxx should be to fix the bug with ripple drawable on foreground?

@danielesegato
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OleksandrGrument this bug is on material components not the design library.

and it is sated a few comments above this that the fix has been released with 1.1.0-alpha01 of material components for android

@asbadve
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if its included in 1.0.0. I am not able to see the ripple effect

Please sign in to comment.