Android QuickContactBadge 带有类似 Android Lollipop 的字母

标签 android image list listview android-contacts

我有一个联系人列表,我想在每个联系人的 QuickContactBadge 中显示他名字的第一个字母。 我可以在运行时创建图像吗?

这就像 Android Lollipop,联系人和拨号器使用带字母的 QuickContactBadge:

Android Lollipop screenshot

最佳答案

我使用函数生成这些图像。

public static Bitmap generateCircleBitmap(Context context, int circleColor, float diameterDP, String text){
    final int textColor = 0xffffffff;

    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    float diameterPixels = diameterDP * (metrics.densityDpi / 160f);
    float radiusPixels = diameterPixels/2;

    // Create the bitmap
    Bitmap output = Bitmap.createBitmap((int) diameterPixels, (int) diameterPixels,
            Bitmap.Config.ARGB_8888);

    // Create the canvas to draw on
    Canvas canvas = new Canvas(output);
    canvas.drawARGB(0, 0, 0, 0);

    // Draw the circle
    final Paint paintC = new Paint();
    paintC.setAntiAlias(true);
    paintC.setColor(circleColor);
    canvas.drawCircle(radiusPixels, radiusPixels, radiusPixels, paintC);

    // Draw the text
    if (text != null && text.length() > 0) {
        final Paint paintT = new Paint();
        paintT.setColor(textColor);
        paintT.setAntiAlias(true);
        paintT.setTextSize(radiusPixels * 2);
        Typeface typeFace = Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Thin.ttf");
        paintT.setTypeface(typeFace);
        final Rect textBounds = new Rect();
        paintT.getTextBounds(text, 0, text.length(), textBounds);
        canvas.drawText(text, radiusPixels - textBounds.exactCenterX(), radiusPixels - textBounds.exactCenterY(), paintT);
    }

    return output;
}

我将联系人的姓名传递给以下 getMaterialColor 函数以选择一种颜色。

private static List<Integer> materialColors = Arrays.asList(
        0xffe57373,
        0xfff06292,
        0xffba68c8,
        0xff9575cd,
        0xff7986cb,
        0xff64b5f6,
        0xff4fc3f7,
        0xff4dd0e1,
        0xff4db6ac,
        0xff81c784,
        0xffaed581,
        0xffff8a65,
        0xffd4e157,
        0xffffd54f,
        0xffffb74d,
        0xffa1887f,
        0xff90a4ae
);

public static int getMaterialColor(Object key) {
    return material.get(Math.abs(key.hashCode()) % materialColors.size());
}

关于Android QuickContactBadge 带有类似 Android Lollipop 的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27082367/

相关文章:

android - 如何检测添加到联系人数据库的新联系人

java - Android SDK 中的 dx.jar 是什么?

image - 圆形 CSS 边框看起来像素化

android - 如何获取android中特定文件夹中的视频文件列表?

python - 在没有 collections.counter 的情况下根据整数值对字典进行排序

android - 实现 VBO 以在 OpenGL ES 2 中渲染 Sprite

android - 当 API 7 上有溢出操作时,菜单按钮会使应用程序崩溃

javascript - 防止Android "long press"保存图片

c# - 使用 C# 通过具有嵌入式图像的 Windows 服务发送自动电子邮件

python - 如果列表包含相同的元素,则在嵌套列表中组合列表?