java 圆形图像与首字母。字体:https://stackoverflow.com/questions/27082367/android-quickcontactbadge-with-letters

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 圆形图像与首字母。字体:https://stackoverflow.com/questions/27082367/android-quickcontactbadge-with-letters相关的知识,希望对你有一定的参考价值。

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());
}
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;
}

以上是关于java 圆形图像与首字母。字体:https://stackoverflow.com/questions/27082367/android-quickcontactbadge-with-letters的主要内容,如果未能解决你的问题,请参考以下文章

如何检查图像资产是不是存在,然后将其用作圆形头像或在圆形头像中写下姓名的第一个字母

PHP获取汉字的拼音(全部与首字母)(转载博博客园2778085001)

设置字体大小,使字母高度等于某个值

从 HTML5 Canvas 上的图像绘制字体

设置UIBarButtonItem为圆形图像轮廓。

计算图像中圆形管道的数量