44import android .graphics .Color ;
55import android .graphics .drawable .Drawable ;
66import android .graphics .drawable .StateListDrawable ;
7+ import android .support .annotation .Nullable ;
78import android .view .View ;
89
910import com .hjq .shape .drawable .ShapeDrawable ;
1920 */
2021public final class ShapeDrawableBuilder {
2122
23+ private static final int NO_COLOR = Color .TRANSPARENT ;
24+
2225 private final View mView ;
2326
2427 private int mShape ;
@@ -37,7 +40,7 @@ public final class ShapeDrawableBuilder {
3740 private float mBottomLeftRadius ;
3841 private float mBottomRightRadius ;
3942
40- private int [] mGradientColor ;
43+ private int [] mGradientColors ;
4144 private boolean mUseLevel ;
4245 private int mAngle ;
4346 private int mGradientType ;
@@ -72,21 +75,21 @@ public ShapeDrawableBuilder(View view, TypedArray typedArray, IShapeDrawableStyl
7275 mShapeWidth = typedArray .getDimensionPixelSize (styleable .getShapeWidthStyleable (), -1 );
7376 mShapeHeight = typedArray .getDimensionPixelSize (styleable .getShapeHeightStyleable (), -1 );
7477
75- mSolidColor = typedArray .getColor (styleable .getSolidColorStyleable (), Color . TRANSPARENT );
78+ mSolidColor = typedArray .getColor (styleable .getSolidColorStyleable (), NO_COLOR );
7679 if (typedArray .hasValue (styleable .getSolidPressedColorStyleable ())) {
77- mSolidPressedColor = typedArray .getColor (styleable .getSolidPressedColorStyleable (), Color . TRANSPARENT );
80+ mSolidPressedColor = typedArray .getColor (styleable .getSolidPressedColorStyleable (), NO_COLOR );
7881 }
7982 if (styleable .getSolidCheckedColorStyleable () > 0 && typedArray .hasValue (styleable .getSolidCheckedColorStyleable ())) {
80- mSolidCheckedColor = typedArray .getColor (styleable .getSolidCheckedColorStyleable (), Color . TRANSPARENT );
83+ mSolidCheckedColor = typedArray .getColor (styleable .getSolidCheckedColorStyleable (), NO_COLOR );
8184 }
8285 if (typedArray .hasValue (styleable .getSolidDisabledColorStyleable ())) {
83- mSolidDisabledColor = typedArray .getColor (styleable .getSolidDisabledColorStyleable (), Color . TRANSPARENT );
86+ mSolidDisabledColor = typedArray .getColor (styleable .getSolidDisabledColorStyleable (), NO_COLOR );
8487 }
8588 if (typedArray .hasValue (styleable .getSolidFocusedColorStyleable ())) {
86- mSolidFocusedColor = typedArray .getColor (styleable .getSolidFocusedColorStyleable (), Color . TRANSPARENT );
89+ mSolidFocusedColor = typedArray .getColor (styleable .getSolidFocusedColorStyleable (), NO_COLOR );
8790 }
8891 if (typedArray .hasValue (styleable .getSolidSelectedColorStyleable ())) {
89- mSolidSelectedColor = typedArray .getColor (styleable .getSolidSelectedColorStyleable (), Color . TRANSPARENT );
92+ mSolidSelectedColor = typedArray .getColor (styleable .getSolidSelectedColorStyleable (), NO_COLOR );
9093 }
9194
9295 int radius = typedArray .getDimensionPixelSize (styleable .getRadiusStyleable (), 0 );
@@ -97,12 +100,12 @@ public ShapeDrawableBuilder(View view, TypedArray typedArray, IShapeDrawableStyl
97100
98101 if (typedArray .hasValue (styleable .getStartColorStyleable ()) && typedArray .hasValue (styleable .getEndColorStyleable ())) {
99102 if (typedArray .hasValue (styleable .getCenterColorStyleable ())) {
100- mGradientColor = new int [] {typedArray .getColor (styleable .getStartColorStyleable (), Color . TRANSPARENT ),
101- typedArray .getColor (styleable .getCenterColorStyleable (), Color . TRANSPARENT ),
102- typedArray .getColor (styleable .getEndColorStyleable (), Color . TRANSPARENT )};
103+ mGradientColors = new int [] {typedArray .getColor (styleable .getStartColorStyleable (), NO_COLOR ),
104+ typedArray .getColor (styleable .getCenterColorStyleable (), NO_COLOR ),
105+ typedArray .getColor (styleable .getEndColorStyleable (), NO_COLOR )};
103106 } else {
104- mGradientColor = new int [] {typedArray .getColor (styleable .getStartColorStyleable (), Color . TRANSPARENT ),
105- typedArray .getColor (styleable .getEndColorStyleable (), Color . TRANSPARENT )};
107+ mGradientColors = new int [] {typedArray .getColor (styleable .getStartColorStyleable (), NO_COLOR ),
108+ typedArray .getColor (styleable .getEndColorStyleable (), NO_COLOR )};
106109 }
107110 }
108111
@@ -113,21 +116,21 @@ public ShapeDrawableBuilder(View view, TypedArray typedArray, IShapeDrawableStyl
113116 mCenterY = typedArray .getFloat (styleable .getCenterYStyleable (), 0.5f );
114117 mGradientRadius = typedArray .getDimensionPixelSize (styleable .getGradientRadiusStyleable (), radius );
115118
116- mStrokeColor = typedArray .getColor (styleable .getStrokeColorStyleable (), Color . TRANSPARENT );
119+ mStrokeColor = typedArray .getColor (styleable .getStrokeColorStyleable (), NO_COLOR );
117120 if (typedArray .hasValue (styleable .getStrokePressedColorStyleable ())) {
118- mStrokePressedColor = typedArray .getColor (styleable .getStrokePressedColorStyleable (), Color . TRANSPARENT );
121+ mStrokePressedColor = typedArray .getColor (styleable .getStrokePressedColorStyleable (), NO_COLOR );
119122 }
120123 if (styleable .getStrokeCheckedColorStyleable () > 0 && typedArray .hasValue (styleable .getStrokeCheckedColorStyleable ())) {
121- mStrokeCheckedColor = typedArray .getColor (styleable .getStrokeCheckedColorStyleable (), Color . TRANSPARENT );
124+ mStrokeCheckedColor = typedArray .getColor (styleable .getStrokeCheckedColorStyleable (), NO_COLOR );
122125 }
123126 if (typedArray .hasValue (styleable .getStrokeDisabledColorStyleable ())) {
124- mStrokeDisabledColor = typedArray .getColor (styleable .getStrokeDisabledColorStyleable (), Color . TRANSPARENT );
127+ mStrokeDisabledColor = typedArray .getColor (styleable .getStrokeDisabledColorStyleable (), NO_COLOR );
125128 }
126129 if (typedArray .hasValue (styleable .getStrokeFocusedColorStyleable ())) {
127- mStrokeFocusedColor = typedArray .getColor (styleable .getStrokeFocusedColorStyleable (), Color . TRANSPARENT );
130+ mStrokeFocusedColor = typedArray .getColor (styleable .getStrokeFocusedColorStyleable (), NO_COLOR );
128131 }
129132 if (typedArray .hasValue (styleable .getStrokeSelectedColorStyleable ())) {
130- mStrokeSelectedColor = typedArray .getColor (styleable .getStrokeSelectedColorStyleable (), Color . TRANSPARENT );
133+ mStrokeSelectedColor = typedArray .getColor (styleable .getStrokeSelectedColorStyleable (), NO_COLOR );
131134 }
132135
133136 mStrokeWidth = typedArray .getDimensionPixelSize (styleable .getStrokeWidthStyleable (), 0 );
@@ -174,7 +177,7 @@ public int getShapeHeight() {
174177
175178 public ShapeDrawableBuilder setSolidColor (int color ) {
176179 mSolidColor = color ;
177- clearGradientColor ();
180+ clearGradientColors ();
178181 return this ;
179182 }
180183
@@ -187,6 +190,7 @@ public ShapeDrawableBuilder setSolidPressedColor(Integer color) {
187190 return this ;
188191 }
189192
193+ @ Nullable
190194 public Integer getSolidPressedColor () {
191195 return mSolidPressedColor ;
192196 }
@@ -196,6 +200,7 @@ public ShapeDrawableBuilder setSolidCheckedColor(Integer color) {
196200 return this ;
197201 }
198202
203+ @ Nullable
199204 public Integer getSolidCheckedColor () {
200205 return mSolidCheckedColor ;
201206 }
@@ -205,6 +210,7 @@ public ShapeDrawableBuilder setSolidDisabledColor(Integer color) {
205210 return this ;
206211 }
207212
213+ @ Nullable
208214 public Integer getSolidDisabledColor () {
209215 return mSolidDisabledColor ;
210216 }
@@ -214,6 +220,7 @@ public ShapeDrawableBuilder setSolidFocusedColor(Integer color) {
214220 return this ;
215221 }
216222
223+ @ Nullable
217224 public Integer getSolidFocusedColor () {
218225 return mSolidFocusedColor ;
219226 }
@@ -223,6 +230,7 @@ public ShapeDrawableBuilder setSolidSelectedColor(Integer color) {
223230 return this ;
224231 }
225232
233+ @ Nullable
226234 public Integer getSolidSelectedColor () {
227235 return mSolidSelectedColor ;
228236 }
@@ -255,22 +263,31 @@ public float getBottomRightRadius() {
255263 return mBottomRightRadius ;
256264 }
257265
258- public ShapeDrawableBuilder setGradientColor (int [] color ) {
259- mGradientColor = color ;
266+ public ShapeDrawableBuilder setGradientColors (int startColor , int endColor ) {
267+ return setGradientColors (new int []{startColor , endColor });
268+ }
269+
270+ public ShapeDrawableBuilder setGradientColors (int startColor , int centerColor , int endColor ) {
271+ return setGradientColors (new int []{startColor , centerColor , endColor });
272+ }
273+
274+ public ShapeDrawableBuilder setGradientColors (int [] colors ) {
275+ mGradientColors = colors ;
260276 return this ;
261277 }
262278
263- public int [] getGradientColor () {
264- return mGradientColor ;
279+ @ Nullable
280+ public int [] getGradientColors () {
281+ return mGradientColors ;
265282 }
266283
267- public boolean isGradientColor () {
268- return mGradientColor != null &&
269- mGradientColor .length > 0 ;
284+ public boolean isGradientColors () {
285+ return mGradientColors != null &&
286+ mGradientColors .length > 0 ;
270287 }
271288
272- public void clearGradientColor () {
273- mGradientColor = null ;
289+ public void clearGradientColors () {
290+ mGradientColors = null ;
274291 }
275292
276293 public ShapeDrawableBuilder setUseLevel (boolean useLevel ) {
@@ -341,6 +358,7 @@ public ShapeDrawableBuilder setStrokePressedColor(Integer color) {
341358 return this ;
342359 }
343360
361+ @ Nullable
344362 public Integer getStrokePressedColor () {
345363 return mStrokePressedColor ;
346364 }
@@ -350,6 +368,7 @@ public ShapeDrawableBuilder setStrokeCheckedColor(Integer color) {
350368 return this ;
351369 }
352370
371+ @ Nullable
353372 public Integer getStrokeCheckedColor () {
354373 return mStrokeCheckedColor ;
355374 }
@@ -359,6 +378,7 @@ public ShapeDrawableBuilder setStrokeDisabledColor(Integer color) {
359378 return this ;
360379 }
361380
381+ @ Nullable
362382 public Integer getStrokeDisabledColor () {
363383 return mStrokeDisabledColor ;
364384 }
@@ -368,6 +388,7 @@ public ShapeDrawableBuilder setStrokeFocusedColor(Integer color) {
368388 return this ;
369389 }
370390
391+ @ Nullable
371392 public Integer getStrokeFocusedColor () {
372393 return mStrokeFocusedColor ;
373394 }
@@ -377,6 +398,7 @@ public ShapeDrawableBuilder setStrokeSelectedColor(Integer color) {
377398 return this ;
378399 }
379400
401+ @ Nullable
380402 public Integer getStrokeSelectedColor () {
381403 return mStrokeSelectedColor ;
382404 }
@@ -489,14 +511,14 @@ public int getShadowOffsetY() {
489511 }
490512
491513 public Drawable buildBackgroundDrawable () {
492- if (!isGradientColor () && mSolidColor == Color . TRANSPARENT && mStrokeColor == Color . TRANSPARENT ) {
514+ if (!isGradientColors () && mSolidColor == NO_COLOR && mStrokeColor == NO_COLOR ) {
493515 return null ;
494516 }
495517
496518 ShapeDrawable defaultDrawable = createShapeDrawable (mSolidColor , mStrokeColor );
497519 // 判断是否设置了渐变色
498- if (isGradientColor ()) {
499- defaultDrawable .setGradientColors (mGradientColor );
520+ if (isGradientColors ()) {
521+ defaultDrawable .setGradientColors (mGradientColors );
500522 }
501523
502524 if (mSolidPressedColor != null && mStrokePressedColor != null &&
@@ -546,7 +568,7 @@ public ShapeDrawable createShapeDrawable(int solidColor, int strokeColor) {
546568 .setSize (mShapeWidth , mShapeHeight )
547569 .setRadius (mTopLeftRadius , mTopRightRadius , mBottomLeftRadius , mBottomRightRadius )
548570 .setSolidColor (solidColor )
549- .setUseLevel (isUseLevel () )
571+ .setUseLevel (mUseLevel )
550572 .setStroke (mStrokeWidth , strokeColor , mDashWidth , mDashGap );
551573
552574 drawable .setGradientAngle (mAngle )
0 commit comments