对于怎么去定义 以及怎么自由,之前的写法完全不方便扩展,这次我任然任由用户创建任意行,但是每一个行是一个model,这个model可以指定是否限制最右边,超出是否自动下一行
而这个bitmap就等于这个打印机纸的宽度,这样计算也不需要了.
float x = current.x;
float y = current.y;
String line = "";
for (int i = 0; i < current.content.length(); i++) {
char c = current.content.charAt(i);
Rect bounds = new Rect();
fontPaint.getTextBounds(c + "", 0, 1, bounds);//230 test_box_1234的宽度
float charWidth = bounds.width();// paint.measureText(String.valueOf(c));
if (x + charWidth > current.limitRight) {
canvas.drawText(line, 0, y, fontPaint);
line = String.valueOf(c);
int fontHeight = bounds.bottom - bounds.top;
y += fontHeight + current.newLineGap;
// y += fontPaint.getFontSpacing()+ current.newLineGap;
x = charWidth;
} else {
line += c;
x += charWidth;
}
public static Bitmap genereateBitmap(PrintLabelModel model) {
int width = model.width;
int height = model.height;
//最终生成的图片
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
Canvas canvas = new Canvas(result);
//先画一整块白色矩形块
canvas.drawRect(0, 0, result.getWidth(), result.getHeight(), paint);
Paint fontPaint = new Paint();
fontPaint.setAntiAlias(true);
fontPaint.setStyle(Paint.Style.FILL);
for (PrintLabelModel.BasePrint info : model.infos) {
//8 dot=1mm. 1英寸=25.4mm.热
if (info instanceof PrintLabelModel.BarCode) {
PrintLabelModel.BarCode current = (PrintLabelModel.BarCode) info;
try {
Bitmap bitmap = PrintBitmapUtil.encodeAsBitmap(current.content, BarcodeFormat.CODE_128, current.width, current.height);
canvas.drawBitmap(bitmap, info.x, info.y, fontPaint);
} catch (WriterException e) {
throw new RuntimeException(e);
}
} else if (info instanceof PrintLabelModel.QRCode) {
PrintLabelModel.QRCode current = (PrintLabelModel.QRCode) info;
Bitmap qrCodeBitmap = com.example.print_sdk.util.BitmapUtils.encode2dAsBitmap(current.content, current.size, current.size, 2);
canvas.drawBitmap(qrCodeBitmap, info.x, info.y, fontPaint);
if (current.showText) {
}
} else if (info instanceof PrintLabelModel.Box) {
PrintLabelModel.Box current = (PrintLabelModel.Box) info;
Paint mypaint = new Paint();
mypaint.setAntiAlias(true);
mypaint.setStyle(Paint.Style.FILL);
mypaint.setColor(Color.BLACK);
mypaint.setStyle(Paint.Style.STROKE);
mypaint.setStrokeWidth(current.border);
float strokeWidth = paint.getStrokeWidth();
canvas.drawRect(current.x - (strokeWidth / 2), current.y - (strokeWidth / 2), current.right - (strokeWidth / 2), current.bottom - (strokeWidth / 2), mypaint);
} else if (info instanceof PrintLabelModel.Text) {
PrintLabelModel.Text current = (PrintLabelModel.Text) info;
fontPaint.setTextSize(current.fontsize);
fontPaint.setColor(Color.BLACK);
if (current.limitRight > 0) {
float x = current.x;
float y = current.y;
String line = "";
for (int i = 0; i < current.content.length(); i++) {
char c = current.content.charAt(i);
Rect bounds = new Rect();
fontPaint.getTextBounds(c + "", 0, 1, bounds);//230 test_box_1234的宽度
float charWidth = bounds.width();// paint.measureText(String.valueOf(c));
if (x + charWidth > current.limitRight) {
canvas.drawText(line, 0, y, fontPaint);
line = String.valueOf(c);
int fontHeight = bounds.bottom - bounds.top;
y += fontHeight + current.newLineGap;
// y += fontPaint.getFontSpacing()+ current.newLineGap;
x = charWidth;
} else {
line += c;
x += charWidth;
}
}
canvas.drawText(line, 0, y, fontPaint);
} else {
canvas.drawText(current.content, info.x, info.y, fontPaint);
}
} else if (info instanceof PrintLabelModel.TextLeftRight) {
PrintLabelModel.TextLeftRight current = (PrintLabelModel.TextLeftRight) info;
fontPaint.setTextSize(current.fontSize);
fontPaint.setColor(Color.BLACK);
String content = current.label + ":" + current.content;
if (current.limitRight > 0) {
float x = current.x;
float y = current.y;
String line = "";
for (int i = 0; i < content.length(); i++) {
char c = content.charAt(i);
float charWidth = fontPaint.measureText(String.valueOf(c));
if (x + charWidth > current.limitRight) {
canvas.drawText(line, 0, y, fontPaint);
line = String.valueOf(c);
y += fontPaint.getFontSpacing() + current.newLineGap;
x = charWidth;
} else {
line += c;
x += charWidth;
}
}
canvas.drawText(line, 0, y, fontPaint);
} else {
canvas.drawText(content
, info.x, info.y, fontPaint);
}
} else {
}
}
paint.setColor(Color.WHITE);
canvas.save();
canvas.restore();
return result;
}
上面还是有点小bug
x = current.x+charWidth;
这样下一行会延续之前的
测量两种方法
fontPaint.getTextBounds(c + "", 0, 1, bounds);//230 test_box_1234的宽度
float charWidth = bounds.width();// paint.measureText(String.valueOf(c));
if (x + charWidth > current.limitRight) {
canvas.drawText(line, 0, y, fontPaint);
line = String.valueOf(c);
int fontHeight = bounds.bottom - bounds.top;
y += fontHeight + current.newLineGap;
// y += fontPaint.getFontSpacing()+ current.newLineGap;
x = current.x+charWidth;
} else {
line += c;
x += charWidth;
}
float charWidth = fontPaint.measureText(String.valueOf(c));
if (x + charWidth > current.limitRight) {
canvas.drawText(line, 0, y, fontPaint);
line = String.valueOf(c);
y += fontPaint.getFontSpacing() + current.newLineGap;
x = current.x+charWidth;
} else {
line += c;
x += charWidth;
}
最后完整的如下
public static Bitmap genereateBitmap(PrintLabelModel model) {
int width = model.width;
int height = model.height;
//最终生成的图片
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
Canvas canvas = new Canvas(result);
//先画一整块白色矩形块
canvas.drawRect(0, 0, result.getWidth(), result.getHeight(), paint);
Paint fontPaint = new Paint();
fontPaint.setAntiAlias(true);
fontPaint.setStyle(Paint.Style.FILL);
for (PrintLabelModel.BasePrint info : model.infos) {
//8 dot=1mm. 1英寸=25.4mm.热
if (info instanceof PrintLabelModel.BarCode) {
PrintLabelModel.BarCode current = (PrintLabelModel.BarCode) info;
try {
Bitmap bitmap = PrintBitmapUtil.encodeAsBitmap(current.content, BarcodeFormat.CODE_128, current.width, current.height);
canvas.drawBitmap(bitmap, info.x, info.y, fontPaint);
} catch (WriterException e) {
throw new RuntimeException(e);
}
} else if (info instanceof PrintLabelModel.QRCode) {
PrintLabelModel.QRCode current = (PrintLabelModel.QRCode) info;
Bitmap qrCodeBitmap = com.example.print_sdk.util.BitmapUtils.encode2dAsBitmap(current.content, current.size, current.size, 2);
canvas.drawBitmap(qrCodeBitmap, info.x, info.y, fontPaint);
if (current.showText) {
}
} else if (info instanceof PrintLabelModel.Box) {
PrintLabelModel.Box current = (PrintLabelModel.Box) info;
Paint mypaint = new Paint();
mypaint.setAntiAlias(true);
mypaint.setStyle(Paint.Style.FILL);
mypaint.setColor(Color.BLACK);
mypaint.setStyle(Paint.Style.STROKE);
mypaint.setStrokeWidth(current.border);
float strokeWidth = paint.getStrokeWidth();
canvas.drawRect(current.x - (strokeWidth / 2), current.y - (strokeWidth / 2), current.right - (strokeWidth / 2), current.bottom - (strokeWidth / 2), mypaint);
} else if (info instanceof PrintLabelModel.Text) {
PrintLabelModel.Text current = (PrintLabelModel.Text) info;
fontPaint.setTextSize(current.fontsize);
fontPaint.setColor(Color.BLACK);
if (current.limitRight > 0) {
float x = current.x;
float y = current.y;
String line = "";
for (int i = 0; i < current.content.length(); i++) {
char c = current.content.charAt(i);
Rect bounds = new Rect();
fontPaint.getTextBounds(c + "", 0, 1, bounds);//230 test_box_1234的宽度
float charWidth = bounds.width();// paint.measureText(String.valueOf(c));
if (x + charWidth > current.limitRight) {
canvas.drawText(line, current.x, y, fontPaint);
line = String.valueOf(c);
int fontHeight = bounds.bottom - bounds.top;
y += fontHeight + current.newLineGap;
// y += fontPaint.getFontSpacing()+ current.newLineGap;
x = current.x + charWidth;
} else {
line += c;
x += charWidth;
}
}
canvas.drawText(line, current.x, y, fontPaint);
} else {
canvas.drawText(current.content, info.x, info.y, fontPaint);
}
} else if (info instanceof PrintLabelModel.TextLeftRight) {
PrintLabelModel.TextLeftRight current = (PrintLabelModel.TextLeftRight) info;
fontPaint.setTextSize(current.fontSize);
fontPaint.setColor(Color.BLACK);
String content = current.label + ":" + current.content;
if (current.limitRight > 0) {
float x = current.x;
float y = current.y;
String line = "";
for (int i = 0; i < content.length(); i++) {
char c = content.charAt(i);
float charWidth = fontPaint.measureText(String.valueOf(c));
if (x + charWidth > current.limitRight) {
canvas.drawText(line, current.x, y, fontPaint);//把之前的给绘制掉
line = String.valueOf(c);
y += fontPaint.getFontSpacing() + current.newLineGap;
x = current.x + charWidth;//第二行
} else {
line += c;
x += charWidth;
}
}
//没超过就是绘制第一行,超过就是绘制末尾的
canvas.drawText(line, current.x, y, fontPaint);
} else {
canvas.drawText(content
, info.x, info.y, fontPaint);
}
} else {
}
}
paint.setColor(Color.WHITE);
canvas.save();
canvas.restore();
return result;
}
用法
PrintLabelModel label = new PrintLabelModel();
int left=10;
int txtBaseY = 85;
int txtIncreatefact = 23;
int fontSize=15;
label.height = 320;
label.width = 480;
label.infos.add(new PrintLabelModel.QRCode(270, 71, 135, "qrcode", false));
label.infos.add(new PrintLabelModel.BarCode(8, 20, 360, 45, "1234567890", true,fontSize));
label.infos.add(new PrintLabelModel.Box(8, 10, 440, 310, 1, true));
label.infos.add(new PrintLabelModel.TextLeftRight(left, txtBaseY, "P/N", "060.01.150002",fontSize));
label.infos.add(new PrintLabelModel.TextLeftRight(left, txtBaseY = txtBaseY + txtIncreatefact, "a/b ", "4444444444444",fontSize));
label.infos.add(new PrintLabelModel.TextLeftRight(left, txtBaseY = txtBaseY + txtIncreatefact, "c/d", "3333XXX",fontSize));
label.infos.add(new PrintLabelModel.TextLeftRight(left, txtBaseY = txtBaseY + txtIncreatefact, "e/f", "2023-03-09",fontSize));
label.infos.add(new PrintLabelModel.TextLeftRight(left, 15, "xff, "W/Vf<=3.4v,abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()你好世界我的你啊不好滚啥特由泥放写连判",fontSize,380));
labelList.add(label);
网友评论