首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Letter头像Gmail Android最佳实践

Letter头像Gmail Android最佳实践
EN

Stack Overflow用户
提问于 2014-06-16 02:36:53
回答 4查看 5.2K关注 0票数 3

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

它应该是这样的:

EN

回答 4

Stack Overflow用户

发布于 2014-06-24 02:55:11

这是我曾经用过的..请尝试根据您的要求进行修改。

代码语言:javascript
复制
    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、字母、填充

票数 7
EN

Stack Overflow用户

发布于 2014-06-16 04:38:11

如果您只询问ListView中左侧的头像。

使用ImageView,如果你有用户头像-放在那里,如果你没有头像-使用.drawText("R")函数绘制画布,并使用setImageDrawable将其放入ImageView

票数 2
EN

Stack Overflow用户

发布于 2014-06-16 02:40:03

从您上面提供的图像可以看出,这可以使用自定义的listview来完成。你正在寻找的头像应该是你的自定义布局中的一个图像视图,并放大到列表视图中。我建议你从这里开始。gravar可以是资源文件夹中可绘制的图像,

http://www.ezzylearning.com/tutorial.aspx?tid=1763429

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24232836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档