像Gmail一样(用代码)生成字母头像的最好方法是什么?这里有一个示例https://drive.google.com/folderview?id=0B0Fhz5fDg1njSmpUakhhZllEWHM&usp=sharing
它应该是这样的:

发布于 2014-06-24 02:55:11
这是我曾经用过的..请尝试根据您的要求进行修改。
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、字母、填充
发布于 2014-06-16 04:38:11
如果您只询问ListView中左侧的头像。
使用ImageView,如果你有用户头像-放在那里,如果你没有头像-使用.drawText("R")函数绘制画布,并使用setImageDrawable将其放入ImageView。
发布于 2014-06-16 02:40:03
从您上面提供的图像可以看出,这可以使用自定义的listview来完成。你正在寻找的头像应该是你的自定义布局中的一个图像视图,并放大到列表视图中。我建议你从这里开始。gravar可以是资源文件夹中可绘制的图像,
http://www.ezzylearning.com/tutorial.aspx?tid=1763429
https://stackoverflow.com/questions/24232836
复制相似问题