Skip to content

Commit 4088fdb

Browse files
ymariancketcham
authored andcommitted
Implement getConstantState in CircularBorderDrawable
This fixes a crash on FloatingActionButtonLollipop when it tries to set the background. It calls mutate which calls it on this drawable. Which calls getConstantState().newDrawable() PiperOrigin-RevId: 205108784
1 parent 5bdaa0d commit 4088fdb

File tree

1 file changed

+44
-12
lines changed

1 file changed

+44
-12
lines changed

lib/java/com/google/android/material/internal/CircularBorderDrawable.java

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
import android.graphics.RectF;
2929
import android.graphics.Shader;
3030
import android.graphics.drawable.Drawable;
31+
import android.support.annotation.ColorInt;
32+
import android.support.annotation.Dimension;
33+
import android.support.annotation.FloatRange;
34+
import android.support.annotation.IntRange;
35+
import android.support.annotation.NonNull;
36+
import android.support.annotation.Nullable;
3137
import android.support.annotation.RestrictTo;
3238
import android.support.v4.graphics.ColorUtils;
3339

@@ -45,39 +51,47 @@ public class CircularBorderDrawable extends Drawable {
4551
final Paint paint;
4652
final Rect rect = new Rect();
4753
final RectF rectF = new RectF();
54+
final CircularBorderState state = new CircularBorderState();
4855

49-
float borderWidth;
56+
@Dimension float borderWidth;
5057

51-
private int topOuterStrokeColor;
52-
private int topInnerStrokeColor;
53-
private int bottomOuterStrokeColor;
54-
private int bottomInnerStrokeColor;
58+
@ColorInt private int topOuterStrokeColor;
59+
@ColorInt private int topInnerStrokeColor;
60+
@ColorInt private int bottomOuterStrokeColor;
61+
@ColorInt private int bottomInnerStrokeColor;
5562

5663
private ColorStateList borderTint;
57-
private int currentBorderTintColor;
64+
@ColorInt private int currentBorderTintColor;
5865

5966
private boolean invalidateShader = true;
6067

68+
@FloatRange(from = 0, to = 360)
6169
private float rotation;
6270

6371
public CircularBorderDrawable() {
6472
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
6573
paint.setStyle(Paint.Style.STROKE);
6674
}
6775

76+
@Nullable
77+
@Override
78+
public ConstantState getConstantState() {
79+
return state;
80+
}
81+
6882
public void setGradientColors(
69-
int topOuterStrokeColor,
70-
int topInnerStrokeColor,
71-
int bottomOuterStrokeColor,
72-
int bottomInnerStrokeColor) {
83+
@ColorInt int topOuterStrokeColor,
84+
@ColorInt int topInnerStrokeColor,
85+
@ColorInt int bottomOuterStrokeColor,
86+
@ColorInt int bottomInnerStrokeColor) {
7387
this.topOuterStrokeColor = topOuterStrokeColor;
7488
this.topInnerStrokeColor = topInnerStrokeColor;
7589
this.bottomOuterStrokeColor = bottomOuterStrokeColor;
7690
this.bottomInnerStrokeColor = bottomInnerStrokeColor;
7791
}
7892

7993
/** Set the border width */
80-
public void setBorderWidth(float width) {
94+
public void setBorderWidth(@Dimension float width) {
8195
if (borderWidth != width) {
8296
borderWidth = width;
8397
paint.setStrokeWidth(width * DRAW_STROKE_WIDTH_MULTIPLE);
@@ -120,7 +134,7 @@ public boolean getPadding(Rect padding) {
120134
}
121135

122136
@Override
123-
public void setAlpha(int alpha) {
137+
public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {
124138
paint.setAlpha(alpha);
125139
invalidateSelf();
126140
}
@@ -211,4 +225,22 @@ private Shader createGradientShader() {
211225
return new LinearGradient(
212226
0, rect.top, 0, rect.bottom, colors, positions, Shader.TileMode.CLAMP);
213227
}
228+
229+
/**
230+
* Dummy implementation of constant state. This drawable doesn't have shared state. Implementing
231+
* so that calls to getConstantState().newDrawable() don't crash on L and M.
232+
*/
233+
private class CircularBorderState extends ConstantState {
234+
235+
@NonNull
236+
@Override
237+
public Drawable newDrawable() {
238+
return CircularBorderDrawable.this;
239+
}
240+
241+
@Override
242+
public int getChangingConfigurations() {
243+
return 0;
244+
}
245+
}
214246
}

0 commit comments

Comments
 (0)