Skip to content

Instantly share code, notes, and snippets.

@muthuraj57
Created December 7, 2016 18:55
Show Gist options
  • Save muthuraj57/0829ec4e3384f38494f7735ff5508ab0 to your computer and use it in GitHub Desktop.
Save muthuraj57/0829ec4e3384f38494f7735ff5508ab0 to your computer and use it in GitHub Desktop.
/*
* Create placeholder drawable
* */
private void setDrawable() {
drawable = new Drawable() {
@Override
public void draw(@NonNull Canvas canvas) {
int centerX = Math.round(canvas.getWidth() * 0.5f);
int centerY = Math.round(canvas.getHeight() * 0.5f);
/*
* To draw text
* */
if (text != null) {
float textWidth = textPaint.measureText(text) * 0.5f;
float textBaseLineHeight = textPaint.getFontMetrics().ascent * -0.4f;
/*
* Draw the background color before drawing initials text
* */
if (shape == RECTANGLE) {
canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint);
} else {
canvas.drawCircle(centerX,
centerY,
Math.max(canvas.getHeight() / 2, textWidth / 2),
paint);
}
/*
* Draw the text above the background color
* */
canvas.drawText(text, centerX - textWidth, centerY + textBaseLineHeight, textPaint);
}
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
}
@Override
public int getOpacity() {
return PixelFormat.UNKNOWN;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment