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.

Revisions

  1. lynfogeek revised this gist Jan 23, 2015. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion InitialDrawable.java
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ public class InitialDrawable extends ShapeDrawable {
    private final int borderThickness;

    private InitialDrawable(Builder builder) {
    super(builder.shape);
    super(new OvalShape());
    height = builder.height;
    width = builder.width;
    radius = builder.radius;
    2 changes: 1 addition & 1 deletion Usage.java
    Original file line number Diff line number Diff line change
    @@ -6,5 +6,5 @@
    .height(Utils.dpToPx(40, ctx))
    .width(Utils.dpToPx(40, ctx));

    Drawable initialDrawable = builder.buildRound(initials.toString(),
    Drawable initialDrawable = builder.build(initials.toString(),
    ctx.getResources().getColor(R.color.blue));
  2. lynfogeek revised this gist Jan 10, 2015. 2 changed files with 4 additions and 22 deletions.
    23 changes: 3 additions & 20 deletions InitialDrawable.java
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,6 @@ public class InitialDrawable extends ShapeDrawable {
    private static final float SHADE_FACTOR = 0.9f;
    private final String text;
    private final int color;
    private final RectShape shape;
    private final int height;
    private final int width;
    private final int fontSize;
    @@ -15,7 +14,6 @@ public class InitialDrawable extends ShapeDrawable {

    private InitialDrawable(Builder builder) {
    super(builder.shape);
    shape = builder.shape;
    height = builder.height;
    width = builder.width;
    radius = builder.radius;
    @@ -50,7 +48,9 @@ public void draw(Canvas canvas) {
    super.draw(canvas);
    Rect r = getBounds();
    if (borderThickness > 0) {
    drawBorder(canvas);
    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);
    @@ -63,12 +63,6 @@ public void draw(Canvas canvas) {
    canvas.restoreToCount(count);
    }

    private void drawBorder(Canvas canvas) {
    RectF rect = new RectF(getBounds());
    rect.inset(borderThickness / 2, borderThickness / 2);
    canvas.drawOval(rect, borderPaint);
    }

    @Override
    public void setAlpha(int alpha) {
    textPaint.setAlpha(alpha);
    @@ -106,7 +100,6 @@ public static class Builder {
    private int width;
    private int height;
    private Typeface font;
    private RectShape shape;
    public int textColor;
    private int fontSize;
    private boolean isBold;
    @@ -121,7 +114,6 @@ private Builder() {
    borderThickness = 0;
    width = -1;
    height = -1;
    shape = new OvalShape();
    font = Typeface.create("sans-serif-light", Typeface.NORMAL);
    fontSize = -1;
    isBold = false;
    @@ -153,7 +145,6 @@ public Builder useFont(Typeface font) {
    return this;
    }


    public Builder fontSize(int size) {
    this.fontSize = size;
    return this;
    @@ -169,14 +160,6 @@ public Builder toUpperCase() {
    return this;
    }

    public Builder beginConfig() {
    return this;
    }

    public Builder endConfig() {
    return this;
    }

    public InitialDrawable build(String text, int color) {
    this.color = color;
    this.text = text;
    3 changes: 1 addition & 2 deletions Usage.java
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,7 @@
    final Context ctx = getContext();

    Builder builder = new Builder();
    builder.beginConfig()
    .fontSize(Utils.dpToPx(24, ctx))
    builder.fontSize(Utils.dpToPx(24, ctx))
    .height(Utils.dpToPx(40, ctx))
    .width(Utils.dpToPx(40, ctx));

  3. lynfogeek revised this gist Jan 10, 2015. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  4. lynfogeek created this gist Jan 10, 2015.
    187 changes: 187 additions & 0 deletions InitialDrawable
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,187 @@

    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 RectShape shape;
    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(builder.shape);
    shape = builder.shape;
    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) {
    drawBorder(canvas);
    }
    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);
    }

    private void drawBorder(Canvas canvas) {
    RectF rect = new RectF(getBounds());
    rect.inset(borderThickness / 2, borderThickness / 2);
    canvas.drawOval(rect, borderPaint);
    }

    @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;
    private RectShape shape;
    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;
    shape = new OvalShape();
    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 Builder beginConfig() {
    return this;
    }

    public Builder endConfig() {
    return this;
    }

    public InitialDrawable build(String text, int color) {
    this.color = color;
    this.text = text;
    return new InitialDrawable(this);
    }
    }

    }
    11 changes: 11 additions & 0 deletions Usage
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    final String initials = "AC";
    final Context ctx = getContext();

    Builder builder = new Builder();
    builder.beginConfig()
    .fontSize(Utils.dpToPx(24, ctx))
    .height(Utils.dpToPx(40, ctx))
    .width(Utils.dpToPx(40, ctx));

    Drawable initialDrawable = builder.buildRound(initials.toString(),
    ctx.getResources().getColor(R.color.blue));