在多行文本上的android绘制描边

时间:2017-09-02 17:10:33

标签: android canvas bitmap android-canvas draw

嗨我想尝试在多行文字上添加笔画,但它不起作用 这是代码 首先绘制填充文本,它的工作正常 但是当试图绘制中风时,它不起作用并给我一些与中风无关的东西

我需要这样的笔画,但需要多行文字 stroke canvas

    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setTextSize(20);
    textPaint.setColor(Color.White);


    textPaint1.setStyle(Paint.Style.STROKE);
    textPaint1.setStrokeWidth(20);
    textPaint1.setColor(Color.GREEN);


    StaticLayout sl = new StaticLayout(
            textLayer.getText(), // - text which will be drawn
            textPaint,
            boundsWidth, // - width of the layout
            Layout.Alignment.ALIGN_CENTER, // - layout alignment
            1, // 1 - text spacing multiply
            1, // 1 - text spacing add
            true); // true - include padding

    int boundsHeight = sl.getHeight();

    int bmpHeight = (int) (canvasHeight * Math.max(TextLayer.Limits.MIN_BITMAP_HEIGHT,
            1.0F * boundsHeight / canvasHeight));

    // create bitmap where text will be drawn
    Bitmap bmp;
    if (reuseBmp != null && reuseBmp.getWidth() == boundsWidth
            && reuseBmp.getHeight() == bmpHeight) {
        // if previous bitmap exists, and it's width/height is the same - reuse it
        bmp = reuseBmp;
        bmp.eraseColor(Color.TRANSPARENT); // erase color when reusing
    } else {
        bmp = Bitmap.createBitmap(boundsWidth, bmpHeight, Bitmap.Config.ARGB_8888);
    }

    StaticLayout sl1 = new StaticLayout(
            textLayer.getText(), // - text which will be drawn
            textPaint1,
            boundsWidth, // - width of the layout
            Layout.Alignment.ALIGN_CENTER, // - layout alignment
            1, // 1 - text spacing multiply
            1, // 1 - text spacing add
            true); // true - include padding

    // calculate height for the entity, min - Limits.MIN_BITMAP_HEIGHT
    int boundsHeight1 = sl1.getHeight();

    int bmpHeight1 = (int) (canvasHeight * Math.max(TextLayer.Limits.MIN_BITMAP_HEIGHT,
            1.0F * boundsHeight1 / canvasHeight));

    Bitmap bmp1;
    if (reuseBmp != null && reuseBmp.getWidth() == boundsWidth
            && reuseBmp.getHeight() == bmpHeight1) {
        // if previous bitmap exists, and it's width/height is the same - reuse it
        bmp1 = reuseBmp;
        bmp1.eraseColor(Color.TRANSPARENT); // erase color when reusing
    } else {
        bmp1 = Bitmap.createBitmap(boundsWidth, bmpHeight, Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bmp);
    canvas.save();
    Canvas canvas1 = new Canvas(bmp1);
    canvas1.save();

    // move text to center if bitmap is bigger that text
    if (boundsHeight < bmpHeight) {
        //calculate Y coordinate - In this case we want to draw the text in the
        //center of the canvas so we move Y coordinate to center.
        float textYCoordinate = (bmpHeight - boundsHeight) / 2;
        canvas.translate(0, textYCoordinate);
    }

    // move text to center if bitmap is bigger that text
    if (boundsHeight1 < bmpHeight1) {
        //calculate Y coordinate - In this case we want to draw the text in the
        //center of the canvas so we move Y coordinate to center.
        float textYCoordinate = (bmpHeight1 - boundsHeight1) / 2;
        canvas1.translate(0, textYCoordinate);
    }

    //draws static layout on canvas
    sl.draw(canvas);
    sl1.draw(canvas1);
    canvas.restore();
    canvas1.restore();

这将结合位图

Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas2 = new Canvas(bmOverlay);
    canvas2.drawBitmap(bmp, new Matrix(), null);
    canvas2.drawBitmap(bmp1, 0, 0, null);
    return bmOverlay;
    return bmp;

1 个答案:

答案 0 :(得分:0)

您需要使用相同的文本创建两个StaticLayout,但其中一个使用文本绘制,另一个使用笔画绘制,然后在相同位置绘制。