在Gmail中生成(以代码形式)字母头像的最佳方法是什么?这里有一个例子https://drive.google.com/folderview?id=0B0Fhz5fDg1njSmpUakhhZllEWHM&usp=sharing
它看起来应该像这样:
public class LetterAvatar extends ColorDrawable {
Paint paint = new Paint();
Rect bounds = new Rect();
String pLetters;
private float ONE_DP = 0.0f;
private Resources pResources;
private int pPadding;
int pSize = 0;
float pMesuredTextWidth;
int pBoundsTextwidth;
int pBoundsTextHeight;
public LetterAvatar (Context context, int color, String letter, int paddingInDp) {
super(color);
this.pLetters = letter;
this.pResources = context.getResources();
ONE_DP = 1 * pResources.getDisplayMetrics().density;
this.pPadding = Math.round(paddingInDp * ONE_DP);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
paint.setAntiAlias(true);
do {
paint.setTextSize(++pSize);
paint.getTextBounds(pLetters, 0, pLetters.length(), bounds);
} while ((bounds.height() < (canvas.getHeight() - pPadding)) && (paint.measureText(pLetters) < (canvas.getWidth() - pPadding)));
paint.setTextSize(pSize);
pMesuredTextWidth = paint.measureText(pLetters);
pBoundsTextHeight = bounds.height();
float xOffset = ((canvas.getWidth() - pMesuredTextWidth) / 2);
float yOffset = (int) (pBoundsTextHeight + (canvas.getHeight() - pBoundsTextHeight) / 2);
paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
paint.setColor(0xffffffff);
canvas.drawText(pLetters, xOffset, yOffset, paint);
}
}
然后在imageview.setdrawable中设置新的LetterAvatar(上下文,colorCode,字母,填充)
ImageView
,如果您有用户头像-将其放置在此处,如果您没有头像-请使用.drawText("R")
函数绘制画布,然后使用ImageView
将其放在setImageDrawable
中。 class AvatarGenerator {
companion object {
lateinit var uiContext: Context
var texSize = 0F
fun avatarImage(context: Context, size: Int, shape: Int, name: String): BitmapDrawable {
uiContext = context
val width = size
val hieght = size
texSize = calTextSize(size)
val label = firstCharacter(name)
val textPaint = textPainter()
val painter = painter()
val areaRect = Rect(0, 0, width, width)
if (shape == 0) {
painter.color = RandomColors().getColor()
} else {
painter.color = Color.TRANSPARENT
}
val bitmap = Bitmap.createBitmap(width, width, ARGB_8888)
val canvas = Canvas(bitmap)
canvas.drawRect(areaRect, painter)
//reset painter
if (shape == 0) {
painter.color = Color.TRANSPARENT
} else {
painter.color = RandomColors().getColor()
}
val bounds = RectF(areaRect)
bounds.right = textPaint.measureText(label, 0, 1)
bounds.bottom = textPaint.descent() - textPaint.ascent()
bounds.left += (areaRect.width() - bounds.right) / 2.0f
bounds.top += (areaRect.height() - bounds.bottom) / 2.0f
canvas.drawCircle(width.toFloat() / 2, hieght.toFloat() / 2, width.toFloat() / 2, painter)
canvas.drawText(label, bounds.left, bounds.top - textPaint.ascent(), textPaint)
return BitmapDrawable(uiContext.resources, bitmap)
}
private fun firstCharacter(name: String): String {
return name.first().toString().toUpperCase()
}
private fun textPainter(): TextPaint {
val textPaint = TextPaint()
textPaint.textSize = texSize * uiContext.resources.displayMetrics.scaledDensity
textPaint.color = Color.WHITE
return textPaint
}
private fun painter(): Paint {
return Paint()
}
private fun calTextSize(size: Int): Float {
return (size / 3.125).toFloat()
}
}
}
然后您可以传递上下文,字符串,大小和形状以生成1/0
imageView.setImageDrawable(
AvatarGenerator.avatarImage(
this,
200,
1,
"Skyways"
)