Skip to content

Commit d619775

Browse files
committed
优化API
1 parent 640d570 commit d619775

1 file changed

Lines changed: 51 additions & 19 deletions

File tree

shapebuilder/src/main/java/com/study/xuan/shapebuilder/shape/ShapeBuilder.java

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Description :封装GradientDrawable替代用shape.xml,减小apk体积
1111
*/
1212

13-
public class ShapeBuilder implements IShape{
13+
public class ShapeBuilder{
1414
private GradientDrawable drawable;
1515
private AttrContainer container;
1616
private boolean isOperate;
@@ -69,6 +69,19 @@ public ShapeBuilder Stroke(int px, int color, int dashWidth, int dashGap) {
6969
return this;
7070
}
7171

72+
/**
73+
* 使用Solid(int color)方法
74+
* @param color -背景颜色
75+
*/
76+
@Deprecated
77+
public ShapeBuilder Soild(int color) {
78+
drawable.setColor(color);
79+
if (container != null) {
80+
container.solid = color;
81+
}
82+
return this;
83+
}
84+
7285
/**
7386
*
7487
* @param color -背景颜色
@@ -197,6 +210,21 @@ private ShapeBuilder GradientInit(GradientDrawable.Orientation orientation, int
197210
return this;
198211
}
199212

213+
/**
214+
* 兼容低版本,重新构造drawable,对应调用operateMethod方法重新build,
215+
* 保证新的drawable与原始drawabel相同
216+
*/
217+
public ShapeBuilder GradientInit(GradientDrawable.Orientation orientation, int... colors) {
218+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
219+
drawable.setOrientation(orientation);
220+
drawable.setColors(colors);
221+
} else {
222+
isOperate = true;
223+
drawable = new GradientDrawable(orientation, colors);
224+
}
225+
return this;
226+
}
227+
200228

201229
/**
202230
* 渐变type
@@ -264,6 +292,10 @@ public void build(View v) {
264292
}
265293
}
266294

295+
public static void clearBg(View v) {
296+
v.setBackgroundResource(0);
297+
}
298+
267299
/**
268300
* 返回构建的drawable
269301
*/
@@ -284,32 +316,32 @@ public GradientDrawable build() {
284316
private void operateMethod() {
285317
if (container != null) {
286318
this.Type(container.type)
287-
.Stroke(container.strokeWidth, container.stokeColor, container.dashWidth,
319+
.Stroke(container.strokeWidth, container.stokeColor, container.dashWidth,
288320
container.dashGap)
289-
.Radius(container.topLeft,container.topRight,container.botLeft,container.botRight)
290-
.setSize(container.width,container.height)
291-
.GradientType(container.gradientType)
292-
.GradientCenter(container.gradientCenterX,container.gradientCenterY)
293-
.GradientRadius(container.gradientRadius);
321+
.Radius(container.topLeft,container.topRight,container.botLeft,container.botRight)
322+
.setSize(container.width,container.height)
323+
.GradientType(container.gradientType)
324+
.GradientCenter(container.gradientCenterX,container.gradientCenterY)
325+
.GradientRadius(container.gradientRadius);
294326
if (container.solid != 0) {
295327
Solid(container.solid);
296328
}
297329
}
298330
}
299331

300332
private class AttrContainer {
301-
private int type;
302-
private int strokeWidth;
303-
private int stokeColor;
304-
private int dashWidth;
305-
private int dashGap;
306-
private int solid;
307-
private float topLeft, topRight, botLeft, botRight;
308-
private int width, height;
309-
private int gradientType;
310-
private float gradientRadius;
333+
public int type;
334+
public int strokeWidth;
335+
public int stokeColor;
336+
public int dashWidth;
337+
public int dashGap;
338+
public int solid;
339+
public float topLeft, topRight, botLeft, botRight;
340+
public int width, height;
341+
public int gradientType;
342+
public float gradientRadius;
311343

312-
private float gradientCenterX, gradientCenterY;
344+
public float gradientCenterX, gradientCenterY;
313345

314346
private void setRadius(float topleft, float topright, float botleft, float botright) {
315347
this.topLeft = topleft;
@@ -318,4 +350,4 @@ private void setRadius(float topleft, float topright, float botleft, float botri
318350
this.botRight = botright;
319351
}
320352
}
321-
}
353+
}

0 commit comments

Comments
 (0)