public static Bitmap headBitmap(String name,int size,String colorHex){
name = name!=null ? ( name.length()>2 ? name.substring(name.length()-2) : name ): "" ;
Bitmap bitmap = Bitmap.createBitmap(size,size,Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bitmap) ;
Rect rect = new Rect(0,0,size,size);//画一个矩形
Paint rectPaint = new Paint();
rectPaint.setColor(Color.parseColor(colorHex));
rectPaint.setStyle(Paint.Style.FILL);
rectPaint.setAntiAlias(true);
canvas.drawCircle(size/2 ,size/2 , size/ 2,rectPaint);
Paint textPaint = new Paint();
textPaint.setColor(Color.WHITE);
textPaint.setTextSize(size /3);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setAntiAlias(true);
textPaint.setTextAlign(Paint.Align.CENTER);
Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
float top = fontMetrics.top;//为基线到字体上边框的距离,即上图中的top
float bottom = fontMetrics.bottom;//为基线到字体下边框的距离,即上图中的bottom
int baseLineY = (int) (rect.centerY() - top/2 - bottom/2);//基线中间点的y轴计算公式
canvas.drawText(name,rect.centerX(),baseLineY,textPaint);
return bitmap ;
}
暂无评论
要发表评论,您必须先 登录