1、新建bitmap的方式很多
public static Bitmap createBitmap(@NonNull Bitmap src) {
throw new RuntimeException("Stub!");
}
public static Bitmap createBitmap(@NonNull Bitmap source, int x, int y, int width, int height) {
throw new RuntimeException("Stub!");
}
public static Bitmap createBitmap(@NonNull Bitmap source, int x, int y, int width, int height, @Nullable Matrix m, boolean filter) {
throw new RuntimeException("Stub!");
}
public static Bitmap createBitmap(int width, int height, @NonNull Bitmap.Config config) {
throw new RuntimeException("Stub!");
}
public static Bitmap createBitmap(@Nullable DisplayMetrics display, int width, int height, @NonNull Bitmap.Config config) {
throw new RuntimeException("Stub!");
}
public static Bitmap createBitmap(int width, int height, @NonNull Bitmap.Config config, boolean hasAlpha) {
throw new RuntimeException("Stub!");
}
public static Bitmap createBitmap(int width, int height, @NonNull Bitmap.Config config, boolean hasAlpha, @NonNull ColorSpace colorSpace) {
throw new RuntimeException("Stub!");
}
public static Bitmap createBitmap(@Nullable DisplayMetrics display, int width, int height, @NonNull Bitmap.Config config, boolean hasAlpha) {
throw new RuntimeException("Stub!");
}
public static Bitmap createBitmap(@Nullable DisplayMetrics display, int width, int height, @NonNull Bitmap.Config config, boolean hasAlpha, @NonNull ColorSpace colorSpace) {
throw new RuntimeException("Stub!");
}
public static Bitmap createBitmap(@NonNull int[] colors, int offset, int stride, int width, int height, @NonNull Bitmap.Config config) {
throw new RuntimeException("Stub!");
}
public static Bitmap createBitmap(@NonNull DisplayMetrics display, @NonNull int[] colors, int offset, int stride, int width, int height, @NonNull Bitmap.Config config) {
throw new RuntimeException("Stub!");
}
public static Bitmap createBitmap(@NonNull int[] colors, int width, int height, Bitmap.Config config) {
throw new RuntimeException("Stub!");
}
public static Bitmap createBitmap(@Nullable DisplayMetrics display, @NonNull int[] colors, int width, int height, @NonNull Bitmap.Config config) {
throw new RuntimeException("Stub!");
}
@NonNull
public static Bitmap createBitmap(@NonNull Picture source) {
throw new RuntimeException("Stub!");
}
@NonNull
public static Bitmap createBitmap(@NonNull Picture source, int width, int height, @NonNull Bitmap.Config config) {
throw new RuntimeException("Stub!");
}
这里选择第4种
public static Bitmap createBitmap(int width, int height, @NonNull Bitmap.Config config) { throw new RuntimeException("Stub!"); } public static enum Config { ALPHA_8, RGB_565, /** @deprecated */ @Deprecated ARGB_4444, ARGB_8888, RGBA_F16, HARDWARE;
private Config() {
}
}
Bitmap bitmap= Bitmap.createBitmap(256,256, Bitmap.Config.ARGB_8888);
2、新建画布,设置字体
//画布
Canvas canvas=new Canvas(bitmap);
//背景色
canvas.drawColor(Color.WHITE);
//文本刷设置
TextPaint textPaint = new TextPaint();
textPaint.setTextSize(16);
textPaint.setColor(Color.BLACK);
//写字板
/*
StaticLayout(CharSequence source, TextPaint paint, int width, Alignment align, float spacingmult, float spacingadd, boolean includepad)
source字符串来源,
paint文本刷,
width宽度,
align对齐:ALIGN_NORMAL, ALIGN_OPPOSITE,ALIGN_CENTER;
spacingmult行间距倍数
spacingadd增加行间距
includepad
*/
StaticLayout layout = new StaticLayout("https://blog.csdn.net/weixin_51380973",textPaint,256, Layout.Alignment.ALIGN_NORMAL,1.0F,0.0F,true);
3、在画布上绘制文本
canvas.translate(0,0);
layout.draw(canvas);
return bitmap;