Android StaticLayout实现主流便签内容生成长图功能

StaticLayout是android中处理文字换行的一个工具类,StaticLayout已经实现了文本绘制换行处理。它跟TextView的效果是一样的,其实TextView也是调用StaticLayout来实现换行的。

public StaticLayout(CharSequence source,
                    int bufstart,
                    int bufend,
                    TextPaint paint,
                    int outerwidth,
                    Layout.Alignment align,
                    float spacingmult,
                    float spacingadd,
                    boolean includepad,
                    TextUtils.TruncateAt ellipsize,
                    int ellipsizedWidth)

1.需要分行的字符串
2.需要分行的字符串从第几的位置开始
3.需要分行的字符串到哪里结束
4.画笔对象
5.layout的宽度,字符串超出宽度时自动换行。
6.layout的对其方式,有ALIGN_CENTER, ALIGN_NORMAL, ALIGN_OPPOSITE 三种。
7.相对行间距,相对字体大小,1.5f表示行间距为1.5倍的字体高度。
8.在基础行距上添加多少
实际行间距等于这两者的和。
9.参数未知
10.从什么位置开始省略
11.超过多少开始省略
需要指出的是这个layout是默认画在Canvas的(0,0)点的,如果需要调整位置只能在draw之前移Canvas的起始坐标
canvas.translate(x,y);

生成长图功能的实现:

public static Bitmap getBitmap(Context context, EditText mEditText) {
        Editable editable = mEditText.getText();
        TextPaint textPaint = new TextPaint();
        textPaint.setARGB(0xFF, 0, 0, 0);
        textPaint.setTextSize(mEditText.getTextSize() - DisplayUtils.sp2px(context, 2));

        // 设置字体
        String fontPath = Cache.read_String(SharedPreferencesKey.KEY_FONT, "");
        if (!TextUtils.isEmpty(fontPath)) {
            File fontFile = new File(fontPath);
            if (fontFile.isFile() && fontFile.exists())
                textPaint.setTypeface(Typeface.createFromFile(fontFile));
        }
        ScreenInfo mScreenInfo = ScreenInfo.getInstance();

        float bitmapWidth = mScreenInfo.getWidthPixels()
                - DisplayUtils.dip2px(context, 10);
        float padding = DisplayUtils.dip2px(context, 15);

        StaticLayout layout = new StaticLayout(editable, textPaint,
                (int) (bitmapWidth - padding * 4), Alignment.ALIGN_NORMAL,
                1.2F, 0.0F, true);

        float bitmapHeight = layout.getHeight() + padding * 10;

        Bitmap bitmap = null;
        try {
            bitmap = Bitmap.createBitmap((int) bitmapWidth, (int) bitmapHeight,
                    Bitmap.Config.ARGB_8888);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
        Canvas paper = new Canvas(bitmap);
        paper.drawColor(ColorSelectView.getColor(2));

        paper.translate(padding * 2, padding * 4);

        layout.draw(paper);

        paper.translate(-padding * 2, -padding * 4);

        Paint mPaint = new Paint(Paint.DITHER_FLAG);
        mPaint.setColor(Color.GRAY);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(DisplayUtils.dip2px(context, 0.5f));

        float offset = DisplayUtils.dip2px(context, 2);

        paper.drawRect(new Rect((int) (padding), (int) (padding * 2),
                (int) (bitmapWidth - padding),
                (int) (bitmapHeight - padding * 4)), mPaint);

        paper.drawRect(new Rect((int) (padding - offset),
                (int) (padding * 2 - offset), (int) (padding),
                (int) (padding * 2)), mPaint);

        paper.drawRect(new Rect((int) (bitmapWidth - padding),
                (int) (padding * 2 - offset),
                (int) (bitmapWidth - padding + offset), (int) (padding * 2)),
                mPaint);

        paper.drawRect(new Rect((int) (padding - offset),
                (int) (bitmapHeight - padding * 4), (int) (padding),
                (int) (bitmapHeight - padding * 4 + offset)), mPaint);

        paper.drawRect(new Rect((int) (bitmapWidth - padding),
                (int) (bitmapHeight - padding * 4), (int) (bitmapWidth
                        - padding + offset),
                (int) (bitmapHeight - padding * 4 + offset)), mPaint);

        paper.drawRect(new Rect((int) (padding + offset),
                (int) (padding * 2 + offset),
                (int) (bitmapWidth - padding - offset), (int) (bitmapHeight
                        - padding * 4 - offset)), mPaint);

        TextPaint fromTextPaint = new TextPaint();
        fromTextPaint.setColor(Color.GRAY);
        fromTextPaint.setTextSize(DisplayUtils.sp2px(context, 12));
        StaticLayout fromLayout = new StaticLayout(Cache.read_String(
                KEY_SHARE_TAIL, context.getString(R.string.share_image_info)),
                fromTextPaint, (int) (bitmapWidth - padding * 3.5),
                Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);
        paper.translate(padding, bitmapHeight - padding * 3);
        fromLayout.draw(paper);

        return bitmap;
    }

static public Boolean printBill(Activity context) {

        try {
            int height = 50 + 14 * 30;
            // 打印高度计算:每多一行加50
            Bitmap bitmap = Bitmap
                    .createBitmap(WIDTH, height, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            canvas.drawColor(Color.WHITE);
            TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
            paint.setStrokeWidth(1);
            paint.setColor(Color.BLACK);
            paint.setTypeface(Typeface.SANS_SERIF);

            String content = "";
            StaticLayout layout;

            // 打印标题--购物小票
            paint.setTextSize(32);
            content = "备注信息";
                layout = new StaticLayout(content, paint, (int) (400),
                    Layout.Alignment.ALIGN_CENTER, 1F, 0, false);
            canvas.translate(0, 0);
            layout.draw(canvas);

            // 分割线
            content = "======================\r\n";
            layout = new StaticLayout(content, paint, (int) (400),
                    Layout.Alignment.ALIGN_NORMAL, 1F, 0, false);
            canvas.translate(0, 50);
            layout.draw(canvas);
            // 打印客户姓名
            content = "客户姓名: " + customerName + "\r\n";
            layout = new StaticLayout(content, paint, (int) (400),
                    Layout.Alignment.ALIGN_NORMAL, 1F, 0, false);
            canvas.translate(0, 50);
            layout.draw(canvas);


            // 打印门店名称
            content = "门店名称: " + orgName + "\r\n";
            layout = new StaticLayout(content, paint, (int) (400),
                    Layout.Alignment.ALIGN_NORMAL, 1F, 0, false);
            canvas.translate(0, 50);
            layout.draw(canvas);

            // 打印款项
            content = "款项类型: " + type + "\r\n";
            layout = new StaticLayout(content, paint, (int) (400),
                    Layout.Alignment.ALIGN_NORMAL, 1F, 0, false);
            canvas.translate(0, 50);
            layout.draw(canvas);

            // 打印ERP单号
            content = "ERP单号: " + erpCode + "\r\n";
            layout = new StaticLayout(content, paint, (int) (400),
                    Layout.Alignment.ALIGN_NORMAL, 1F, 0, false);
            canvas.translate(0, 50);
            layout.draw(canvas);

            // 打印HY单号
            content = "HY单号: " + jtlOrderNo + "\r\n";
            layout = new StaticLayout(content, paint, (int) (400),
                    Layout.Alignment.ALIGN_NORMAL, 1F, 0, false);
            canvas.translate(0, 50);
            layout.draw(canvas);



            canvas.save(Canvas.ALL_SAVE_FLAG);
            canvas.restore();
            String path = Environment.getExternalStorageDirectory()
                    + "/image.png";
            System.out.println(path);
            FileOutputStream os = new FileOutputStream(new File(path));
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
            os.flush();
            os.close();
            AppHelper.callPrint(context, path);
            return true;

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }
打赏