Add shape theme to MaterialCardView.

When content can be clipped to the shape (round rect, Lollipop+), the content will be clipped to the interior of the stroke.

When preventCornerOverlap is true, if the content cannot be clipped, extra padding is added such that the entirety of the content is inside the stroke. Any contentPadding is additional to this padding. In order to keep centered content centered, the same amount of padding is added in each direction when preventCornerOverlap is true.

PiperOrigin-RevId: 222456398
This commit is contained in:
isabellekim 2018-11-21 16:55:29 -05:00 committed by ldjcmu
parent bd2a4484c3
commit 46e18968d7
5 changed files with 244 additions and 87 deletions

View File

@ -90,7 +90,7 @@ public class MaterialCardView extends CardView {
context, attrs, R.styleable.MaterialCardView, defStyleAttr, DEF_STYLE_RES);
// Loads and sets background drawable attributes.
cardViewHelper = new MaterialCardViewHelper(this);
cardViewHelper = new MaterialCardViewHelper(this, attrs, defStyleAttr, DEF_STYLE_RES);
// Get the card background color that CardView read from the attributes.
cardViewHelper.setCardBackgroundColor(super.getCardBackgroundColor());
cardViewHelper.loadFromAttributes(attributes);
@ -143,10 +143,59 @@ public class MaterialCardView extends CardView {
@Override
public void setRadius(float radius) {
super.setRadius(radius);
cardViewHelper.updateCornerRadius();
cardViewHelper.setCornerRadius(radius);
updateContentLayout();
}
@Override
public float getRadius() {
return cardViewHelper.getCornerRadius();
}
float getCardViewRadius() {
return MaterialCardView.super.getRadius();
}
@Override
public void setContentPadding(int left, int top, int right, int bottom) {
cardViewHelper.setUserContentPadding(left, top, right, bottom);
}
@Override
public int getContentPaddingLeft() {
return cardViewHelper.getUserContentPadding().left;
}
@Override
public int getContentPaddingTop() {
return cardViewHelper.getUserContentPadding().top;
}
@Override
public int getContentPaddingRight() {
return cardViewHelper.getUserContentPadding().right;
}
@Override
public int getContentPaddingBottom() {
return cardViewHelper.getUserContentPadding().bottom;
}
@Override
public void setCardBackgroundColor(@ColorInt int color) {
cardViewHelper.setCardBackgroundColor(ColorStateList.valueOf(color));
}
@Override
public void setCardBackgroundColor(@Nullable ColorStateList color) {
cardViewHelper.setCardBackgroundColor(color);
}
@Override
public ColorStateList getCardBackgroundColor() {
return cardViewHelper.getCardBackgroundColor();
}
@Override
public void setLayoutParams(ViewGroup.LayoutParams params) {
super.setLayoutParams(params);
@ -163,21 +212,6 @@ public class MaterialCardView extends CardView {
cardViewHelper.updateClickable();
}
@Override
public void setCardBackgroundColor(@ColorInt int color) {
cardViewHelper.setCardBackgroundColor(ColorStateList.valueOf(color));
}
@Override
public void setCardBackgroundColor(@Nullable ColorStateList color) {
cardViewHelper.setCardBackgroundColor(color);;
}
@Override
public ColorStateList getCardBackgroundColor() {
return cardViewHelper.getCardBackgroundColor();
}
@Override
public void setCardElevation(float elevation) {
super.setCardElevation(elevation);
@ -194,12 +228,14 @@ public class MaterialCardView extends CardView {
public void setUseCompatPadding(boolean useCompatPadding) {
super.setUseCompatPadding(useCompatPadding);
cardViewHelper.updateInsets();
cardViewHelper.updateContentPadding();
}
@Override
public void setPreventCornerOverlap(boolean preventCornerOverlap) {
super.setPreventCornerOverlap(preventCornerOverlap);
cardViewHelper.updateInsets();
cardViewHelper.updateContentPadding();
}
@Override
@ -259,4 +295,8 @@ public class MaterialCardView extends CardView {
super.setBackgroundDrawable(drawable);
}
void setContentPaddingInternal(int left, int top, int right, int bottom) {
super.setContentPadding(left, top, right, bottom);
}
}

View File

@ -39,10 +39,15 @@ import android.support.annotation.ColorInt;
import android.support.annotation.Dimension;
import android.support.annotation.Nullable;
import android.support.annotation.RestrictTo;
import android.support.annotation.StyleRes;
import com.google.android.material.ripple.RippleUtils;
import com.google.android.material.shape.CornerTreatment;
import com.google.android.material.shape.CutCornerTreatment;
import com.google.android.material.shape.MaterialShapeDrawable;
import com.google.android.material.shape.RoundedCornerTreatment;
import com.google.android.material.shape.ShapeAppearanceModel;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewOutlineProvider;
@ -84,51 +89,63 @@ class MaterialCardViewHelper {
@ColorInt private int strokeColor;
@ColorInt private int rippleColor;
@Dimension private int strokeWidth;
private final Rect userContentPadding = new Rect();
private final ShapeAppearanceModel shapeAppearanceModel; // Shared by background, stroke & ripple
private final MaterialShapeDrawable bgDrawable; // Will always wrapped in an InsetDrawable
private final MaterialShapeDrawable strokeDrawable; // Will always wrapped in an InsetDrawable
/** Either a {@link RippleDrawable} when using framework ripple or a {@link StateListDrawable}. */
@Nullable private Drawable rippleDrawable;
/** Layers stroke and ripple drawables. */
@Nullable private LayerDrawable clickableForegroundDrawable;
/**
* When not using framework drawable, this is used for the {@link StateListDrawable} stored in
* {@link #rippleDrawable}.
*/
@Nullable private MaterialShapeDrawable compatRippleDrawable;
private final ShapeAppearanceModel shapeAppearanceModelInsetByStroke;
private final MaterialShapeDrawable drawableInsetByStroke;
private final Rect temporaryBounds = new Rect();
// If card is clickable, this is the clickableForegroundDrawable otherwise it is the
// strokeDrawable
private Drawable fgDrawable;
private boolean isBackgroundOverwritten = false;
public MaterialCardViewHelper(MaterialCardView card) {
public MaterialCardViewHelper(
MaterialCardView card, AttributeSet attrs, int defStyleAttr, @StyleRes int defStyleRes) {
materialCardView = card;
shapeAppearanceModel = new ShapeAppearanceModel();
bgDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
bgDrawable = new MaterialShapeDrawable(card.getContext(), attrs, defStyleAttr, defStyleRes);
shapeAppearanceModel = bgDrawable.getShapeAppearanceModel();
bgDrawable.setShadowColor(Color.DKGRAY);
strokeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
strokeDrawable.setFillColor(ColorStateList.valueOf(Color.TRANSPARENT));
fgDrawable = materialCardView.isClickable() ? getClickableForeground() : strokeDrawable;
TypedArray cardViewAttributes =
card.getContext()
.obtainStyledAttributes(attrs, R.styleable.CardView, defStyleAttr, R.style.CardView);
if (cardViewAttributes.hasValue(R.styleable.CardView_cardCornerRadius)) {
shapeAppearanceModel.setCornerRadius(
cardViewAttributes.getDimension(R.styleable.CardView_cardCornerRadius, 0));
}
shapeAppearanceModelInsetByStroke = new ShapeAppearanceModel(shapeAppearanceModel);
drawableInsetByStroke = new MaterialShapeDrawable(shapeAppearanceModelInsetByStroke);
}
public void loadFromAttributes(TypedArray attributes) {
// If cardCornerRadius is set, let it override the shape appearance.
strokeColor =
attributes.getColor(R.styleable.MaterialCardView_strokeColor, DEFAULT_STROKE_VALUE);
strokeWidth = attributes.getDimensionPixelSize(R.styleable.MaterialCardView_strokeWidth, 0);
adjustShapeAppearanceModelInsetByStroke();
rippleColor = getRippleColor();
updateRippleColor();
updateCornerRadius();
updateElevation();
updateStroke();
materialCardView.setBackgroundInternal(insetDrawable(bgDrawable));
materialCardView.setForeground(insetDrawable(fgDrawable));
adjustContentPadding(strokeWidth);
updateContentPadding();
}
boolean isBackgroundOverwritten() {
@ -158,10 +175,10 @@ class MaterialCardViewHelper {
return;
}
int strokeWidthDelta = strokeWidth - this.strokeWidth;
this.strokeWidth = strokeWidth;
adjustShapeAppearanceModelInsetByStroke();
updateStroke();
adjustContentPadding(strokeWidthDelta);
updateContentPadding();
}
@Dimension
@ -177,6 +194,15 @@ class MaterialCardViewHelper {
return bgDrawable.getFillColor();
}
void setUserContentPadding(int left, int top, int right, int bottom) {
userContentPadding.set(left, top, right, bottom);
updateContentPadding();
}
Rect getUserContentPadding() {
return userContentPadding;
}
void updateClickable() {
Drawable previousFgDrawable = fgDrawable;
fgDrawable = materialCardView.isClickable() ? getClickableForeground() : strokeDrawable;
@ -185,10 +211,22 @@ class MaterialCardViewHelper {
}
}
void updateCornerRadius() {
shapeAppearanceModel.setCornerRadius(materialCardView.getRadius());
void setCornerRadius(float cornerRadius) {
shapeAppearanceModel.setCornerRadius(cornerRadius);
shapeAppearanceModelInsetByStroke.setCornerRadius(cornerRadius - strokeWidth);
bgDrawable.invalidateSelf();
fgDrawable.invalidateSelf();
if (shouldAddCornerPaddingOutsideCardBackground()
|| shouldAddCornerPaddingInsideCardBackground()) {
updateContentPadding();
}
if (shouldAddCornerPaddingOutsideCardBackground()) {
updateInsets();
}
}
float getCornerRadius() {
return shapeAppearanceModel.getTopLeftCorner().getCornerSize();
}
void updateElevation() {
@ -227,20 +265,36 @@ class MaterialCardViewHelper {
// To draw the stroke outside the outline, call {@link View#setClipToOutline} on the child
// rather than on the card view.
materialCardView.setClipToOutline(false);
contentView.setClipToOutline(true);
contentView.setOutlineProvider(
// TODO: Derive outline from ShapeAppearanceModel
new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setRoundRect(
0,
0,
view.getWidth(),
view.getHeight(),
materialCardView.getRadius() - strokeWidth);
}
});
if (canClipToOutline()) {
contentView.setClipToOutline(true);
contentView.setOutlineProvider(
new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
temporaryBounds.set(0, 0, view.getWidth(), view.getHeight());
drawableInsetByStroke.setBounds(temporaryBounds);
drawableInsetByStroke.getOutline(outline);
}
});
} else {
contentView.setClipToOutline(false);
contentView.setOutlineProvider(null);
}
}
private void adjustShapeAppearanceModelInsetByStroke() {
shapeAppearanceModelInsetByStroke
.getTopLeftCorner()
.setCornerSize(shapeAppearanceModel.getTopLeftCorner().getCornerSize() - strokeWidth);
shapeAppearanceModelInsetByStroke
.getTopRightCorner()
.setCornerSize(shapeAppearanceModel.getTopRightCorner().getCornerSize() - strokeWidth);
shapeAppearanceModelInsetByStroke
.getBottomRightCorner()
.setCornerSize(shapeAppearanceModel.getBottomRightCorner().getCornerSize() - strokeWidth);
shapeAppearanceModelInsetByStroke
.getBottomLeftCorner()
.setCornerSize(shapeAppearanceModel.getBottomLeftCorner().getCornerSize() - strokeWidth);
}
/**
@ -273,21 +327,8 @@ class MaterialCardViewHelper {
boolean isPreLollipop = Build.VERSION.SDK_INT < VERSION_CODES.LOLLIPOP;
if (isPreLollipop || materialCardView.getUseCompatPadding()) {
// Calculate the shadow padding used by CardView
boolean addInsetForCorners = materialCardView.getPreventCornerOverlap() && !isPreLollipop;
insetVertical =
(int)
Math.ceil(
calculateVerticalPadding(
materialCardView.getMaxCardElevation(),
materialCardView.getRadius(),
addInsetForCorners));
insetHorizontal =
(int)
Math.ceil(
calculateHorizontalPadding(
materialCardView.getMaxCardElevation(),
materialCardView.getRadius(),
addInsetForCorners));
insetVertical = (int) Math.ceil(calculateVerticalBackgroundPadding());
insetHorizontal = (int) Math.ceil(calculateHorizontalBackgroundPadding());
}
return new InsetDrawable(
originalDrawable, insetHorizontal, insetVertical, insetHorizontal, insetVertical) {
@ -300,24 +341,73 @@ class MaterialCardViewHelper {
};
}
/** Copied from {@link CardView} implementation. */
static float calculateVerticalPadding(
float maxShadowSize, float cornerRadius, boolean addPaddingForCorners) {
if (addPaddingForCorners) {
return (float) (maxShadowSize * CARD_VIEW_SHADOW_MULTIPLIER + (1 - COS_45) * cornerRadius);
} else {
return maxShadowSize * CARD_VIEW_SHADOW_MULTIPLIER;
}
/**
* Calculates the amount of padding that should be added above and below the background shape.
* This should only be called pre-lollipop or when using compat padding. This accounts for shadow
* and corner padding when they are added outside the background.
*/
private float calculateVerticalBackgroundPadding() {
return materialCardView.getMaxCardElevation() * CARD_VIEW_SHADOW_MULTIPLIER
+ (shouldAddCornerPaddingOutsideCardBackground() ? calculateActualCornerPadding() : 0);
}
/** Copied from {@link CardView} implementation. */
static float calculateHorizontalPadding(
float maxShadowSize, float cornerRadius, boolean addPaddingForCorners) {
if (addPaddingForCorners) {
return (float) (maxShadowSize + (1 - COS_45) * cornerRadius);
} else {
return maxShadowSize;
/**
* Calculates the amount of padding that should be added to the left and right of the background
* shape. This should only be called pre-lollipop or when using compat padding. This accounts for
* shadow and corner padding when they are added outside the background.
*/
private float calculateHorizontalBackgroundPadding() {
return materialCardView.getMaxCardElevation()
+ (shouldAddCornerPaddingOutsideCardBackground() ? calculateActualCornerPadding() : 0);
}
private boolean canClipToOutline() {
return VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP && shapeAppearanceModel.isRoundRect();
}
private float getParentCardViewCalculatedCornerPadding() {
if (materialCardView.getPreventCornerOverlap()
&& (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP || materialCardView.getUseCompatPadding())) {
return (float) ((1 - COS_45) * materialCardView.getCardViewRadius());
}
return 0f;
}
private boolean shouldAddCornerPaddingInsideCardBackground() {
return materialCardView.getPreventCornerOverlap() && !canClipToOutline();
}
private boolean shouldAddCornerPaddingOutsideCardBackground() {
return materialCardView.getPreventCornerOverlap()
&& canClipToOutline()
&& materialCardView.getUseCompatPadding();
}
/**
* Calculates the amount of padding required between the card background shape and the card
* content such that the entire content is within the bounds of the card background shape.
*
* <p>This should only be called when either {@link
* #shouldAddCornerPaddingOutsideCardBackground()} or {@link
* #shouldAddCornerPaddingInsideCardBackground()} returns true.
*/
private float calculateActualCornerPadding() {
return Math.max(
Math.max(
calculateCornerPaddingForCornerTreatment(shapeAppearanceModel.getTopLeftCorner()),
calculateCornerPaddingForCornerTreatment(shapeAppearanceModel.getTopRightCorner())),
Math.max(
calculateCornerPaddingForCornerTreatment(shapeAppearanceModel.getBottomRightCorner()),
calculateCornerPaddingForCornerTreatment(shapeAppearanceModel.getBottomLeftCorner())));
}
private float calculateCornerPaddingForCornerTreatment(CornerTreatment treatment) {
if (treatment instanceof RoundedCornerTreatment) {
return (float) ((1 - COS_45) * treatment.getCornerSize());
} else if (treatment instanceof CutCornerTreatment) {
return treatment.getCornerSize() / 2;
}
return 0;
}
private Drawable getClickableForeground() {
@ -332,14 +422,26 @@ class MaterialCardViewHelper {
return clickableForegroundDrawable;
}
/** Guarantee at least enough content padding to account for the stroke width. */
private void adjustContentPadding(int strokeWidthDelta) {
int contentPaddingLeft = materialCardView.getContentPaddingLeft() + strokeWidthDelta;
int contentPaddingTop = materialCardView.getContentPaddingTop() + strokeWidthDelta;
int contentPaddingRight = materialCardView.getContentPaddingRight() + strokeWidthDelta;
int contentPaddingBottom = materialCardView.getContentPaddingBottom() + strokeWidthDelta;
materialCardView.setContentPadding(
contentPaddingLeft, contentPaddingTop, contentPaddingRight, contentPaddingBottom);
/**
* Guarantee at least enough content padding to account for the stroke width and support
* preventing corner overlap for shaped backgrounds.
*/
void updateContentPadding() {
boolean includeCornerPadding =
shouldAddCornerPaddingInsideCardBackground()
|| shouldAddCornerPaddingOutsideCardBackground();
// The amount with which to adjust the user provided content padding to account for stroke and
// shape corners.
int contentPaddingOffset =
(int)
(strokeWidth
+ (includeCornerPadding ? calculateActualCornerPadding() : 0)
- getParentCardViewCalculatedCornerPadding());
materialCardView.setContentPaddingInternal(
userContentPadding.left + contentPaddingOffset,
userContentPadding.top + contentPaddingOffset,
userContentPadding.right + contentPaddingOffset,
userContentPadding.bottom + contentPaddingOffset);
}
private int getRippleColor() {

View File

@ -30,6 +30,14 @@
<!-- Width of the stroke path of the card. Default is 0. Attribute type definition is in
resources package. -->
<attr name="strokeWidth"/>
<!-- Shape appearance style reference for MaterialCardView. Attribute declaration is in the
shape package. -->
<attr name="shapeAppearance"/>
<!-- Shape appearance overlay style reference for MaterialCardView. To be used to augment
attributes declared in the shapeAppearance. Attribute declaration is in the shape package.
-->
<attr name="shapeAppearanceOverlay"/>
</declare-styleable>
</resources>

View File

@ -19,10 +19,11 @@
<!-- Defines the base Material card style. -->
<style name="Widget.MaterialComponents.CardView" parent="CardView">
<item name="enforceMaterialTheme">true</item>
<item name="cardElevation">@dimen/mtrl_card_elevation</item>
<item name="cardBackgroundColor">?attr/colorSurface</item>
<item name="cardCornerRadius">@dimen/mtrl_card_corner_radius</item>
<item name="enforceMaterialTheme">true</item>
<item name="cardCornerRadius">@null</item>
<item name="shapeAppearance">?attr/shapeAppearanceMediumComponent</item>
</style>
</resources>

View File

@ -18,6 +18,8 @@ package com.google.android.material.shape;
import com.google.android.material.R;
import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
@ -25,6 +27,7 @@ import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.AttrRes;
import android.support.annotation.Dimension;
import android.support.annotation.RestrictTo;
import android.support.annotation.StyleRes;
import com.google.android.material.internal.Experimental;
import com.google.android.material.internal.ThemeEnforcement;
@ -393,8 +396,11 @@ public class ShapeAppearanceModel {
/**
* Checks Corner and Edge treatments to see if we can use {@link Canvas#drawRoundRect(RectF,float,
* float, Paint)} "} to draw this model.
*
* @hide
*/
boolean isRoundRect() {
@RestrictTo(LIBRARY_GROUP)
public boolean isRoundRect() {
boolean hasDefaultEdges =
leftEdge.getClass().equals(EdgeTreatment.class)
&& rightEdge.getClass().equals(EdgeTreatment.class)