Drawable合集<第一篇>:TextDrawable
2019-12-15 本文已影响0人
NoBugException
TextDrawable
是专门用于修饰文本的Drawable,代码来源于github,只是在源码的基础上做了一些修改。
TextDrawable.java
/**
* @author amulya
* @datetime 14 Oct 2014, 3:53 PM
*/
public class TextDrawable extends ShapeDrawable {
private final Paint textPaint;
private final String text;
private final int textColor;
private final int fontSize;
private final Paint borderPaint;
private final int borderThickness;
private final int borderColor;
private final Paint shapePaint;
private final int shapeColor;
private final RectShape shape;
private final int height;
private final int width;
private final float radius;
private TextDrawable(Builder builder) {
super(builder.shape);
// shape properties
shape = builder.shape;
height = builder.height;
width = builder.width;
radius = builder.radius;
// text and color
text = builder.toUpperCase ? builder.text.toUpperCase() : builder.text;
textColor = builder.textColor;
// text paint settings
fontSize = builder.fontSize;
textPaint = new Paint();
textPaint.setColor(builder.textColor);
textPaint.setAntiAlias(true);
textPaint.setDither(true);
textPaint.setFakeBoldText(builder.isBold);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setTypeface(builder.font);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setStrokeWidth(builder.borderThickness);
// border paint settings
borderThickness = builder.borderThickness;
borderColor = builder.borderColor;
borderPaint = new Paint();
borderPaint.setAntiAlias(true);
borderPaint.setDither(true);
borderPaint.setColor(borderColor);
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(borderThickness);
// shape paint settings
shapeColor = builder.shapeColor;
shapePaint = new Paint();
shapePaint.setAntiAlias(true);
shapePaint.setDither(true);
shapePaint.setColor(shapeColor);
shapePaint.setStyle(Paint.Style.FILL);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
//画圆
drawShape(canvas);
// 画边框
if (borderThickness > 0) {
drawBorder(canvas);
}
// 画文字
drawText(canvas);
}
/**
* 画边框
* @param canvas
*/
private void drawBorder(Canvas canvas) {
RectF rect = new RectF(getBounds());
rect.inset(borderThickness/2-1 > 0 ? borderThickness/2-1 : 0, borderThickness/2-1 > 0 ? borderThickness/2-1 : 0);
if (shape instanceof OvalShape) {
canvas.drawOval(rect, borderPaint);
} else if (shape instanceof RoundRectShape) {
canvas.drawRoundRect(rect, radius, radius, borderPaint);
} else {
canvas.drawRect(rect, borderPaint);
}
}
/**
* 画形状
* @param canvas
*/
private void drawShape(Canvas canvas){
RectF rect = new RectF(getBounds());
rect.inset(borderThickness/2, borderThickness/2);
if (shape instanceof OvalShape) {
canvas.drawOval(rect, shapePaint);
} else if (shape instanceof RoundRectShape) {
canvas.drawRoundRect(rect, radius, radius, shapePaint);
} else {
canvas.drawRect(rect, shapePaint);
}
}
/**
* 画文字
* @param canvas
*/
private void drawText(Canvas canvas){
Rect rect = new Rect(getBounds());
int width = this.width < 0 ? rect.width() : this.width;
int height = this.height < 0 ? rect.height() : this.height;
int fontSize = this.fontSize < 0 ? (Math.min(width, height) / 2) : this.fontSize;
textPaint.setTextSize(fontSize);
canvas.drawText(text, width / 2, height / 2 - ((textPaint.descent() + textPaint.ascent()) / 2), textPaint);
}
@Override
public void setAlpha(int alpha) {
textPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
textPaint.setColorFilter(cf);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
@Override
public int getIntrinsicWidth() {
return width;
}
@Override
public int getIntrinsicHeight() {
return height;
}
public static IShapeBuilder builder() {
return new Builder();
}
public static class Builder implements IConfigBuilder, IShapeBuilder, IBuilder {
private String text;
private int shapeColor;
private int borderThickness;
private int borderColor;
private int width;
private int height;
private Typeface font;
private RectShape shape;
public int textColor;
private int fontSize;
private boolean isBold;
private boolean toUpperCase;
public float radius;
private Builder() {
text = "";
shapeColor = Color.GRAY;
textColor = Color.WHITE;
borderThickness = 0;
borderColor = Color.WHITE;
width = -1;
height = -1;
shape = new RectShape();
font = Typeface.create("sans-serif-light", Typeface.NORMAL);
fontSize = -1;
isBold = false;
toUpperCase = false;
}
public IConfigBuilder width(int width) {
this.width = width;
return this;
}
public IConfigBuilder height(int height) {
this.height = height;
return this;
}
public IConfigBuilder textColor(int color) {
this.textColor = color;
return this;
}
public IConfigBuilder withBorder(int thickness) {
this.borderThickness = thickness;
return this;
}
public IConfigBuilder borderColor(int borderColor) {
this.borderColor = borderColor;
return this;
}
public IConfigBuilder useFont(Typeface font) {
this.font = font;
return this;
}
public IConfigBuilder fontSize(int size) {
this.fontSize = size;
return this;
}
public IConfigBuilder bold() {
this.isBold = true;
return this;
}
public IConfigBuilder toUpperCase() {
this.toUpperCase = true;
return this;
}
@Override
public IConfigBuilder beginConfig() {
return this;
}
@Override
public IShapeBuilder endConfig() {
return this;
}
@Override
public IBuilder rect() {
this.shape = new RectShape();
return this;
}
@Override
public IBuilder round() {
this.shape = new OvalShape();
return this;
}
@Override
public IBuilder roundRect(int radius) {
this.radius = radius;
float[] radii = {radius, radius, radius, radius, radius, radius, radius, radius};
this.shape = new RoundRectShape(radii, null, null);
return this;
}
@Override
public TextDrawable buildRect(String text, int color) {
rect();
return build(text, color);
}
@Override
public TextDrawable buildRoundRect(String text, int color, int radius) {
roundRect(radius);
return build(text, color);
}
@Override
public TextDrawable buildRound(String text, int color) {
round();
return build(text, color);
}
@Override
public TextDrawable build(String text, int color) {
this.shapeColor = color;
this.text = text;
return new TextDrawable(this);
}
}
public interface IConfigBuilder {
public IConfigBuilder width(int width);
public IConfigBuilder height(int height);
public IConfigBuilder textColor(int color);
public IConfigBuilder withBorder(int thickness);
public IConfigBuilder borderColor(int borderColor);
public IConfigBuilder useFont(Typeface font);
public IConfigBuilder fontSize(int size);
public IConfigBuilder bold();
public IConfigBuilder toUpperCase();
public IShapeBuilder endConfig();
}
public static interface IBuilder {
public TextDrawable build(String text, int color);
}
public static interface IShapeBuilder {
public IConfigBuilder beginConfig();
public IBuilder rect();
public IBuilder round();
public IBuilder roundRect(int radius);
public TextDrawable buildRect(String text, int color);
public TextDrawable buildRoundRect(String text, int color, int radius);
public TextDrawable buildRound(String text, int color);
}
}
【矩形、圆角矩形、圆形文本表示】
代码:
//矩形文本
TextDrawable drawable1 = TextDrawable.builder().buildRect("A", Color.RED);
imageview1.setImageDrawable(drawable1);
//圆角矩形文本
TextDrawable drawable2 = TextDrawable.builder().buildRoundRect("B", Color.RED, 10);
imageview2.setImageDrawable(drawable2);
//圆形文本
TextDrawable drawable3 = TextDrawable.builder().buildRound("C", Color.RED);
imageview3.setImageDrawable(drawable3);
效果:
图片.png【加入边框】
代码:
//矩形文本
TextDrawable drawable1 = TextDrawable.builder()
.beginConfig()
.withBorder(10) /* thickness in px */
.borderColor(Color.parseColor("#E76F9B"))
.endConfig()
.buildRect("A", Color.parseColor("#6BB4EE"));
imageview1.setImageDrawable(drawable1);
//圆角矩形文本
TextDrawable drawable2 = TextDrawable.builder()
.beginConfig()
.withBorder(10) /* thickness in px */
.borderColor(Color.parseColor("#E76F9B"))
.endConfig()
.buildRoundRect("B", Color.parseColor("#6BB4EE"), 50);
imageview2.setImageDrawable(drawable2);
//圆形文本
TextDrawable drawable3 = TextDrawable.builder()
.beginConfig()
.withBorder(10) /* thickness in px */
.borderColor(Color.parseColor("#E76F9B"))
.endConfig()
.buildRound("C", Color.parseColor("#6BB4EE"));
imageview3.setImageDrawable(drawable3);
效果:
图片.png在绘制边框中有这样一句代码:
rect.inset(borderThickness/2-1 > 0 ? borderThickness/2-1 : 0, borderThickness/2-1 > 0 ? borderThickness/2-1 : 0);
理想的代码是这样的:
rect.inset(borderThickness/2, borderThickness/2);
但是为了避免一像素的误差,所以需要减去一。
【文字相关】
代码:
//矩形文本
TextDrawable drawable1 = TextDrawable.builder()
.beginConfig()
.textColor(Color.BLACK)
.useFont(Typeface.DEFAULT)
.fontSize(100) /* size in px */
.bold()
.toUpperCase()
.endConfig()
.buildRect("C", Color.parseColor("#6BB4EE"));
imageview1.setImageDrawable(drawable1);
//圆角矩形文本
TextDrawable drawable2 = TextDrawable.builder()
.beginConfig()
.textColor(Color.BLACK)
.useFont(Typeface.DEFAULT_BOLD)
.fontSize(100) /* size in px */
.toUpperCase()
.endConfig()
.buildRoundRect("C", Color.parseColor("#6BB4EE"), 50);
imageview2.setImageDrawable(drawable2);
//圆形文本
TextDrawable drawable3 = TextDrawable.builder()
.beginConfig()
.textColor(Color.BLACK)
.useFont(Typeface.SERIF)
.fontSize(100) /* size in px */
.bold()
.toUpperCase()
.endConfig()
.buildRound("C", Color.parseColor("#6BB4EE"));
imageview3.setImageDrawable(drawable3);
文字的操作有:文字颜色
、文字字体
、文字大小
、文字样式
、大小写转换
。
效果:
图片.png【形状和颜色分离】
代码:
//矩形文本
TextDrawable drawable1 = TextDrawable.builder()
.rect()
.build("A", Color.parseColor("#FF6BB4EE"));
imageview1.setImageDrawable(drawable1);
//圆角矩形文本
TextDrawable drawable2 = TextDrawable.builder()
.roundRect(20)
.build("B", Color.parseColor("#FF6BB4EE"));
imageview2.setImageDrawable(drawable2);
//圆形文本
TextDrawable drawable3 = TextDrawable.builder()
.round()
.build("C", Color.parseColor("#FF6BB4EE"));
imageview3.setImageDrawable(drawable3);
效果:
图片.png【设置宽度和高度】
代码:
//矩形文本
TextDrawable drawable1 = TextDrawable.builder()
.beginConfig()
.width(60)
.height(60)
.endConfig()
.buildRect("A", Color.parseColor("#6BB4EE"));
imageview1.setImageDrawable(drawable1);
//圆角矩形文本
TextDrawable drawable2 = TextDrawable.builder()
.beginConfig()
.width(60)
.height(60)
.endConfig()
.buildRoundRect("B", Color.parseColor("#6BB4EE"), 50);
imageview2.setImageDrawable(drawable2);
//圆形文本
TextDrawable drawable3 = TextDrawable.builder()
.beginConfig()
.width(60)
.height(60)
.endConfig()
.buildRound("C", Color.parseColor("#6BB4EE"));
imageview3.setImageDrawable(drawable3);
效果:
[...省略...]
[本章完...]