答案 0 :(得分:3)
你可以使用
public Bitmap createImage(int width, int height, int color, String name) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint2 = new Paint();
paint2.setColor(color);
canvas.drawRect(0F, 0F, (float) width, (float) height, paint2);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(72);
paint.setTextScaleX(1);
canvas.drawText(name, 75 - 25, 75 + 20, paint);
return bitmap;
}
答案 1 :(得分:3)
我使用了TextDrawable库,发现它非常有用。 这个轻量级的库提供了像Gmail应用程序一样的字母/文本图像。它扩展了Drawable类,因此可以与现有/自定义/网络ImageView类一起使用。还包括用于创建drawable的流畅界面和可自定义的ColorGenerator。
答案 2 :(得分:0)
设置圆形ImageView的图像视图
ImageView imageView = (ImageView)findViewById(R.id.img);
imageView.setImageBitmap(ImageHelper.createImageRounded(getApplicationContext(), 150, 150, "S"));
使用画布创建圆形图像的方法
public static Bitmap createImageRounded(Context context, int width, int height, String name)
{
Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Paint paintCicle = new Paint();
Paint paintText = new Paint();
Rect rect = new Rect(0, 0, width, height);
RectF rectF = new RectF(rect);
float density = context.getResources().getDisplayMetrics().density;
float roundPx = 100*density;
paintCicle.setColor(Color.LTGRAY);
paintCicle.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
// Set Border For Circle
paintCicle.setStyle(Paint.Style.STROKE);
paintCicle.setStrokeWidth(4.0f);
canvas.drawRoundRect(rectF, roundPx, roundPx, paintCicle);
paintText.setColor(Color.GRAY);
paintText.setTextSize(72);
canvas.drawText(name, 75 - 23, 75 + 25, paintText);
return output;
}