android自由坐标绘制的研究之超出自动换行等研究笔记

该代码段展示了如何在Android中动态生成Bitmap,根据指定的限制条件处理文本换行。它遍历不同类型的打印信息(如条形码、二维码、文本等),计算字符宽度,判断是否超出限制,适时换行并在Canvas上绘制。同时,代码中也包含了对不同类型内容(如Box、Text、TextLeftRight)的处理逻辑。
摘要由CSDN通过智能技术生成

对于怎么去定义,以及怎么自由,这次我任由用户创建任意行,但是每一个行是一个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);



最后

如果想要成为架构师或想突破20~30K薪资范畴,那就不要局限在编码,业务,要会选型、扩展,提升编程思维。此外,良好的职业规划也很重要,学习的习惯很重要,但是最重要的还是要能持之以恒,任何不能坚持落实的计划都是空谈。

如果你没有方向,这里给大家分享一套由阿里高级架构师编写的《Android八大模块进阶笔记》,帮大家将杂乱、零散、碎片化的知识进行体系化的整理,让大家系统而高效地掌握Android开发的各个知识点。
在这里插入图片描述
相对于我们平时看的碎片化内容,这份笔记的知识点更系统化,更容易理解和记忆,是严格按照知识体系编排的。

全套视频资料:

一、面试合集

在这里插入图片描述
二、源码解析合集
在这里插入图片描述

三、开源框架合集
在这里插入图片描述
欢迎大家一键三连支持,若需要文中资料,直接扫描文末CSDN官方认证微信卡片免费领取↓↓↓

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值