Skip to content

Commit

Permalink
Add builder to ShapeAppearanceModel
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 265806696
  • Loading branch information
cketcham authored and wcshi committed Aug 29, 2019
1 parent 79953b0 commit ca91442
Show file tree
Hide file tree
Showing 7 changed files with 530 additions and 95 deletions.
33 changes: 33 additions & 0 deletions lib/java/com/google/android/material/shape/CornerTreatment.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,25 @@
package com.google.android.material.shape;

import androidx.annotation.NonNull;
import android.util.Log;

/**
* A basic corner treatment (a single point which does not affect the shape).
*
* <p>If you create your own custom corner treatment and want to use {@link
* ShapeAppearanceModel.Builder#setCornerRadius(float)} which updates the size of the corner in the
* builder, you will need to override {@link #withSize(float)} to return an instance of your custom
* corner treatment for the given size.
*
* <p>Note: For corner treatments which result in a concave shape, the parent view must disable
* clipping of children by calling {@link android.view.ViewGroup#setClipChildren(boolean)}, or by
* setting `android:clipChildren="false"` in xml. `clipToPadding` may also need to be false if there
* is any padding on the parent that could intersect the shadow.
*/
public class CornerTreatment implements Cloneable {

private static final String TAG = "CornerTreatment";

protected float cornerSize;

public CornerTreatment() {
Expand Down Expand Up @@ -76,4 +84,29 @@ public CornerTreatment clone() {
// cloning, so all subclasses of CornerTreatment will support cloning.
}
}

/**
* Returns a new instance of this {@link CornerTreatment} for the given cornerSize. Extending
* classes should override this method to return an instance of their custom class. This is used
* by the builder when calling a method to set the size of the corner treatment such as {@link
* ShapeAppearanceModel.Builder#setCornerRadius(float)}.
*/
@NonNull
public CornerTreatment withSize(float cornerSize) {
return new CornerTreatment(cornerSize);
}

/**
* Checks that the {@link CornerTreatment} returned by calling withSize() is the class we expect.
*/
@NonNull
public static CornerTreatment withSizeAndCornerClassCheck(
@NonNull CornerTreatment treatment, float cornerSize) {
CornerTreatment updatedTreatment = treatment.withSize(cornerSize);
if (!updatedTreatment.getClass().equals(treatment.getClass())) {
Log.w(
TAG, "CornerTreatments should override withSize() to return an instance of their class");
}
return updatedTreatment;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ public void getCornerPath(float angle, float interpolation, @NonNull ShapePath s
// on api levels 21 and 22. Using sin() with 90 - angle is helping for now.
(float) (Math.sin(Math.toRadians(90 - angle)) * cornerSize * interpolation));
}

@NonNull
@Override
public CutCornerTreatment withSize(float cornerSize) {
return new CutCornerTreatment(cornerSize);
}
}
13 changes: 1 addition & 12 deletions lib/java/com/google/android/material/shape/EdgeTreatment.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* setting `android:clipChildren="false"` in xml. `clipToPadding` may also need to be false if there
* is any padding on the parent that could intersect the shadow.
*/
public class EdgeTreatment implements Cloneable {
public class EdgeTreatment {

/**
* @deprecated Does not support interpolation. Use {@link #getEdgePath(float, float, float,
Expand Down Expand Up @@ -63,15 +63,4 @@ public void getEdgePath(
float length, float center, float interpolation, @NonNull ShapePath shapePath) {
shapePath.lineTo(length, 0);
}

@NonNull
@Override
public EdgeTreatment clone() {
try {
return (EdgeTreatment) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError(e); // This should never happen, because EdgeTreatment handles the
// cloning, so all subclasses of EdgeTreatment will support cloning.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ public void getCornerPath(float angle, float interpolation, @NonNull ShapePath s
shapePath.reset(0, radius * interpolation, ShapePath.ANGLE_LEFT, 180 - angle);
shapePath.addArc(0, 0, 2 * radius * interpolation, 2 * radius * interpolation, 180, angle);
}

@NonNull
@Override
public RoundedCornerTreatment withSize(float cornerSize) {
return new RoundedCornerTreatment(cornerSize);
}
}
Loading

0 comments on commit ca91442

Please sign in to comment.