Skip to content

Instantly share code, notes, and snippets.

@lynfogeek
Last active August 12, 2016 03:56
Show Gist options
  • Save lynfogeek/f4e3c37b14a92fcfd7c0 to your computer and use it in GitHub Desktop.
Save lynfogeek/f4e3c37b14a92fcfd7c0 to your computer and use it in GitHub Desktop.
InitialDrawable: A contact-like drawable to display up to 2 letters in a colored circle
public class InitialDrawable extends ShapeDrawable {
private final Paint textPaint;
private final Paint borderPaint;
private static final float SHADE_FACTOR = 0.9f;
private final String text;
private final int color;
private final int height;
private final int width;
private final int fontSize;
private final float radius;
private final int borderThickness;
private InitialDrawable(Builder builder) {
super(new OvalShape());
height = builder.height;
width = builder.width;
radius = builder.radius;
text = builder.toUpperCase ? builder.text.toUpperCase() : builder.text;
color = builder.color;
fontSize = builder.fontSize;
textPaint = new Paint();
textPaint.setColor(builder.textColor);
textPaint.setAntiAlias(true);
textPaint.setFakeBoldText(builder.isBold);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setTypeface(builder.font);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setStrokeWidth(builder.borderThickness);
borderThickness = builder.borderThickness;
borderPaint = new Paint();
borderPaint.setColor(getDarkerShade(color));
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(borderThickness);
Paint paint = getPaint();
paint.setColor(color);
}
private int getDarkerShade(int color) {
return Color.rgb((int)(SHADE_FACTOR * Color.red(color)),
(int)(SHADE_FACTOR * Color.green(color)),
(int)(SHADE_FACTOR * Color.blue(color)));
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
Rect r = getBounds();
if (borderThickness > 0) {
RectF rect = new RectF(getBounds());
rect.inset(borderThickness / 2, borderThickness / 2);
canvas.drawOval(rect, borderPaint);
}
int count = canvas.save();
canvas.translate(r.left, r.top);
int width = this.width < 0 ? r.width() : this.width;
int height = this.height < 0 ? r.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);
canvas.restoreToCount(count);
}
@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 Builder builder() {
return new Builder();
}
public static class Builder {
private String text;
private int color;
private int borderThickness;
private int width;
private int height;
private Typeface font;
public int textColor;
private int fontSize;
private boolean isBold;
private boolean toUpperCase;
public float radius;
private Builder() {
text = "";
color = Color.GRAY;
textColor = Color.WHITE;
borderThickness = 0;
width = -1;
height = -1;
font = Typeface.create("sans-serif-light", Typeface.NORMAL);
fontSize = -1;
isBold = false;
toUpperCase = false;
}
public Builder width(int width) {
this.width = width;
return this;
}
public Builder height(int height) {
this.height = height;
return this;
}
public Builder textColor(int color) {
this.textColor = color;
return this;
}
public Builder withBorder(int thickness) {
this.borderThickness = thickness;
return this;
}
public Builder useFont(Typeface font) {
this.font = font;
return this;
}
public Builder fontSize(int size) {
this.fontSize = size;
return this;
}
public Builder bold() {
this.isBold = true;
return this;
}
public Builder toUpperCase() {
this.toUpperCase = true;
return this;
}
public InitialDrawable build(String text, int color) {
this.color = color;
this.text = text;
return new InitialDrawable(this);
}
}
}
final String initials = "AC";
final Context ctx = getContext();
Builder builder = new Builder();
builder.fontSize(Utils.dpToPx(24, ctx))
.height(Utils.dpToPx(40, ctx))
.width(Utils.dpToPx(40, ctx));
Drawable initialDrawable = builder.build(initials.toString(),
ctx.getResources().getColor(R.color.blue));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment