public class EasyVerticalCenterSpan extends ReplacementSpan {
private final float fontSizeSp; //字体大小sp
private final float paintColor;
public EasyVerticalCenterSpan(float fontSizeSp, float paintColor) {
this.fontSizeSp = fontSizeSp;
this.paintColor = paintColor;
}
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt
fm) {
paint.setTextSize(fontSizeSp);
text = text.subSequence(start, end);
return (int) paint.measureText(text.toString());
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int
y, int bottom, Paint paint) {
text = text.subSequence(start, end);
paint.setTextSize(fontSizeSp);
paint.setColor((int) paintColor);
Paint.FontMetricsInt fm = paint.getFontMetricsInt();
canvas.drawText(text.toString(), x, y - (((y + fm.descent + y + fm.ascent) >> 1) - ((bottom + top) >> 1)), paint); //此处重新计算y坐标,使字体居中
}
}
ShapeBuilder
public class ShapeBuilder {
private GradientDrawable drawable;
private AttrContainer container;
private boolean isOperate;
private ShapeBuilder() {
drawable = new GradientDrawable();
}
public static ShapeBuilder create() {
return new ShapeBuilder();
}
public ShapeBuilder Type(int type) {
drawable.setShape(type);
if (container != null) {
container.type = type;
}
return this;
}
public ShapeBuilder Stroke(int px, int color) {
drawable.setStroke(px, color);
if (container != null) {
container.stokewidth = px;
container.stokeColor = color;
}
return this;
}
public ShapeBuilder Stroke(int px, int color, int dashWidth, int dashGap) {
drawable.setStroke(px, color, dashWidth, dashGap);
if (container != null) {
container.stokewidth = px;
container.stokeColor = color;
container.dashWidth = dashWidth;
container.dashGap = dashGap;
}
return this;
}
public ShapeBuilder Soild(int color) {
drawable.setColor(color);
if (container != null) {
container.soild = color;
}
return this;
}
public ShapeBuilder Radius(float px) {
drawable.setCornerRadius(px);
if (container != null) {
container.setRadius(px, px, px, px);
}
return this;
}
@Deprecated
public ShapeBuilder Radius(float topleft, float topright, float botleft, float botright) {
drawable.setCornerRadii(new float[]{topleft, topleft, topright, topright, botleft,
botleft, botright, botright});
if (container != null) {
container.setRadius(topleft, topright, botleft, botright);
}
return this;
}
public ShapeBuilder RoundRadius(float topleft, float topright, float botleft, float botright) {
drawable.setCornerRadii(new float[]{topleft, topleft, topright, topright, botright,
botright, botleft, botleft});
if (container != null) {
container.setRadius(topleft, topright, botleft, botright);
}
return this;
}
public ShapeBuilder Gradient(int startColor, int centerColor, int endColor) {
return GradientInit(GradientDrawable.Orientation.TOP_BOTTOM, startColor, centerColor,
endColor);
}
public ShapeBuilder Gradient(int angle, int startColor, int centerColor, int endColor) {
angle = angle % 360;
GradientDrawable.Orientation orientation = null;
switch (angle) {
case 0:
orientation = GradientDrawable.Orientation.LEFT_RIGHT;
break;
case 45:
orientation = GradientDrawable.Orientation.BL_TR;
break;
case 90:
orientation = GradientDrawable.Orientation.BOTTOM_TOP;
break;
case 135:
orientation = GradientDrawable.Orientation.BR_TL;
break;
case 180:
orientation = GradientDrawable.Orientation.RIGHT_LEFT;
break;
case 225:
orientation = GradientDrawable.Orientation.TR_BL;
break;
case 270:
orientation = GradientDrawable.Orientation.TOP_BOTTOM;
break;
case 315:
orientation = GradientDrawable.Orientation.TL_BR;
break;
}
return Gradient(orientation, startColor, centerColor, endColor);
}
public ShapeBuilder Gradient(GradientDrawable.Orientation orientation, int startColor, int
centerColor, int endColor) {
return GradientInit(orientation, startColor, centerColor, endColor);
}
private ShapeBuilder GradientInit(GradientDrawable.Orientation orientation, int startColor, int
centerColor, int endColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
drawable.setOrientation(orientation);
drawable.setColors(new int[]{startColor, centerColor, endColor});
} else {
isOperate = true;
drawable = new GradientDrawable(orientation, new int[]{startColor, centerColor,
endColor});
}
return this;
}
public ShapeBuilder GradientInit(GradientDrawable.Orientation orientation, int... colors) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
drawable.setOrientation(orientation);
drawable.setColors(colors);
} else {
isOperate = true;
drawable = new GradientDrawable(orientation, colors);
}
return this;
}
public ShapeBuilder GradientType(int type) {
drawable.setGradientType(type);
if (container != null) {
container.gradientType = type;
}
return this;
}
public ShapeBuilder GradientCenter(float x, float y) {
drawable.setGradientCenter(x, y);
if (container != null) {
container.gradientCenterX = x;
container.gradientCenterY = y;
}
return this;
}
public ShapeBuilder GradientRadius(float radius) {
drawable.setGradientRadius(radius);
if (container != null) {
container.gradinetRadius = radius;
}
return this;
}
public ShapeBuilder setSize(int width, int height) {
drawable.setSize(width, height);
if (container != null) {
container.width = width;
container.height = height;
}
return this;
}
public void build(View v) {
build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
v.setBackground(drawable);
} else {
v.setBackgroundDrawable(drawable);
}
}
public static void clearBg(View v) {
v.setBackgroundResource(0);
}
public GradientDrawable build() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
return drawable;
} else {
if (isOperate) {
operateMethod();
}
}
return drawable;
}
private void operateMethod() {
if (container != null) {
this.Type(container.type)
.Stroke(container.stokewidth, container.stokeColor, container.dashWidth,
container.dashGap)
.Radius(container.topLeft, container.topRight, container.botLeft, container.botRight)
.setSize(container.width, container.height)
.GradientType(container.gradientType)
.GradientCenter(container.gradientCenterX, container.gradientCenterY)
.GradientRadius(container.gradinetRadius);
if (container.soild != 0) {
Soild(container.soild);
}
}
}
private class AttrContainer {
public int type;
public int stokewidth;
public int stokeColor;
public int dashWidth;
public int dashGap;
public int soild;
public float topLeft, topRight, botLeft, botRight;
public int width, height;
public int gradientType;
public float gradinetRadius;
public float gradientCenterX, gradientCenterY;
private void setRadius(float topleft, float topright, float botleft, float botright) {
this.topLeft = topleft;
this.topRight = topright;
this.botLeft = botleft;
this.botRight = botright;
}
}
}
SpanContainer
public class SpanContainer {
public List spans;
public int start;
public int end;
public int flag;
public SpanContainer(List spans, int start, int end, int flag) {
this.spans = spans;
this.start = start;
this.end = end;
this.flag = flag;
}
public SpanContainer(Object spans, int start, int end, int flag) {
this.spans = new ArrayList<>();
this.spans.add(spans);
this.start = start;
this.end = end;
this.flag = flag;
}
}
评论列表(0条)