gpt4 book ai didi

android - 字母头像Gmail Android最佳实践

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:38 26 4
gpt4 key购买 nike

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

它应该是这样的:

Gmail letter avatar

最佳答案

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

    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(context, colorCode, letters, padding)

关于android - 字母头像Gmail Android最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24232836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com