2014-11-22 1 views
3

연락처 목록이 있으며 각 연락처의 QuickContactBadge에 자신의 이름의 첫 글자를 표시하고 싶습니다. 런타임에 이미지를 만들 수 있습니까? Android Lollipop과 같은 글자로 된 Android QuickContactBadge

은 연락처 및 전화 걸기가 문자로 QuickContactBadge를 사용하는 안드로이드 롤리팝, 같다 :

Android Lollipop screenshot

+4

https://github.com/amulyakhare/TextDrawable – SunnySydeUp

답변

6

나는이 이미지를 생성하는 함수를 사용합니다.

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

도와 줘서 고마워. 누가이 코드를 사용할 것입니까? "fonts/Roboto-Thin.ttf"와 조심하십시오. API 16 이하에서는 사용할 수 없습니다. 따라서 수동으로 추가해야합니다. –