From 1054efa3d85f8c2547e3a495ddd3ad20482a8b83 Mon Sep 17 00:00:00 2001 From: Vijay Rawat Date: Thu, 19 Mar 2015 01:19:46 +0530 Subject: [PATCH] Support of drawing image/icon in centre instead of text. Example use case is SMS/Messages App: If message is from a known contact their initials are drawn else a default icon. --- .../textdrawable/TextDrawable.java | 55 ++++++++++++++++++- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/com/amulyakhare/textdrawable/TextDrawable.java b/library/src/main/java/com/amulyakhare/textdrawable/TextDrawable.java index db42f8b..baee3b7 100644 --- a/library/src/main/java/com/amulyakhare/textdrawable/TextDrawable.java +++ b/library/src/main/java/com/amulyakhare/textdrawable/TextDrawable.java @@ -1,6 +1,8 @@ package com.amulyakhare.textdrawable; import android.graphics.*; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.OvalShape; import android.graphics.drawable.shapes.RectShape; @@ -23,6 +25,7 @@ public class TextDrawable extends ShapeDrawable { private final int fontSize; private final float radius; private final int borderThickness; + private Bitmap bitmap; private TextDrawable(Builder builder) { super(builder.shape); @@ -59,6 +62,11 @@ private TextDrawable(Builder builder) { Paint paint = getPaint(); paint.setColor(color); + //custom centre drawable + if(builder.drawable != null) { + bitmap = ((BitmapDrawable)builder.drawable).getBitmap(); + } + } private int getDarkerShade(int color) { @@ -79,15 +87,21 @@ public void draw(Canvas canvas) { } int count = canvas.save(); - canvas.translate(r.left, r.top); + if (bitmap == null) { + canvas.translate(r.left, r.top); + } // draw text 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); + if (bitmap == null) { + textPaint.setTextSize(fontSize); + canvas.drawText(text, width / 2, height / 2 - ((textPaint.descent() + textPaint.ascent()) / 2), textPaint); + } else { + canvas.drawBitmap(bitmap, (width - bitmap.getWidth()) / 2, (height - bitmap.getHeight()) / 2, null); + } canvas.restoreToCount(count); } @@ -162,6 +176,8 @@ public static class Builder implements IConfigBuilder, IShapeBuilder, IBuilder { public float radius; + public Drawable drawable; + private Builder() { text = ""; color = Color.GRAY; @@ -252,24 +268,49 @@ public TextDrawable buildRect(String text, int color) { return build(text, color); } + @Override + public TextDrawable buildRect(Drawable drawable, int color) { + rect(); + return build(drawable, color); + } + @Override public TextDrawable buildRoundRect(String text, int color, int radius) { roundRect(radius); return build(text, color); } + @Override + public TextDrawable buildRoundRect(Drawable drawable, int color, int radius) { + roundRect(radius); + return build(drawable, color); + } + @Override public TextDrawable buildRound(String text, int color) { round(); return build(text, color); } + @Override + public TextDrawable buildRound(Drawable drawable, int color) { + round(); + return build(drawable, color); + } + @Override public TextDrawable build(String text, int color) { this.color = color; this.text = text; return new TextDrawable(this); } + + @Override + public TextDrawable build(Drawable drawable, int color) { + this.drawable = drawable; + this.color = color; + return new TextDrawable(this); + } } public interface IConfigBuilder { @@ -295,6 +336,8 @@ public interface IConfigBuilder { public static interface IBuilder { public TextDrawable build(String text, int color); + + public TextDrawable build(Drawable drawable, int color); } public static interface IShapeBuilder { @@ -309,8 +352,14 @@ public static interface IShapeBuilder { public TextDrawable buildRect(String text, int color); + public TextDrawable buildRect(Drawable drawable, int color); + public TextDrawable buildRoundRect(String text, int color, int radius); + public TextDrawable buildRoundRect(Drawable drawable, int color, int radius); + public TextDrawable buildRound(String text, int color); + + public TextDrawable buildRound(Drawable drawable, int color); } } \ No newline at end of file