Android的QuickContactBadge与Android这样棒棒糖信 [英] Android QuickContactBadge with letters like Android Lollipop

查看:505
本文介绍了Android的QuickContactBadge与Android这样棒棒糖信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a list of contacts and I want to show in each contact's QuickContactBadge the first letter of his name. Can I create the images in runtime?

这是Android这样的棒棒糖,其中,联系人和拨号器使用QuickContactBadge字母:

This is like Android Lollipop, where contacts and dialer uses QuickContactBadge with letters:

Android的棒棒糖截图

推荐答案

我用一个函数来生成这些图像。

I use a function to generate these images.

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功能来选择颜色。

I pass the contact's name into the following getMaterialColor function to select a color.

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这样棒棒糖信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆