【问题标题】:Letter avatar like Gmail Android best practice像 Gmail Android 最佳实践的字母头像
【发布时间】:2014-08-05 15:17:51
【问题描述】:

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

应该是这样的:

【问题讨论】:

  • 只需一个TextView 就足够了。你试过什么?

标签: android gmail photo avatar letter


【解决方案1】:

这是我用过一次的..请根据您的要求尝试修改。

    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 中设置 new LetterAvatar(context, colorCode, letters, padding)

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 如何将 Canvas 设置为 ImageView? setImageDrawable,具体参数是什么?
    • Bitmap bm = Bitmap.createBitmap(width, height,Bimap.Config.ARGB_8888); Canvas cv = new Canvas(bm); //Here write letter or set avatar. imgView.setImageBitmap(bm);
    【解决方案3】:

    从您上面提供的图像来看,这可以使用自定义列表视图来完成。您要查找的头像应该是自定义布局中的图像视图,并膨胀到列表视图中。我建议你从这里开始。 gravar 可以是资源文件夹中可绘制的图像,

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

    【讨论】:

    • 我的意思是如何在代码中动态生成这样一个字母头像。并且不要使用任何图像和可绘制对象。例如。 FrameLayout 和 TextView 里面有字母
    【解决方案4】:

    下面是一个生成圆形和方形图像头像的类。来源是here

        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"
          )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-30
      • 2014-05-23
      • 1970-01-01
      • 2010-10-18
      • 2023-03-16
      • 2014-09-20
      • 1970-01-01
      • 2013-09-24
      相关资源
      最近更新 更多