使用自定义高度在Android上绘制文本 - Android

时间:2015-04-14 16:07:50

标签: java android canvas drawing2d

我正在使用以下代码在画布上绘制文字:

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);

        Paint paint = new Paint();
        Rect bounds = new Rect();
        paint.setColor(Color.BLACK);
        paint.setTextSize(50);

        paint.getTextBounds(first, 0, first.length(), bounds);
        canvas.drawText(first, (canvas.getWidth() - bounds.width()) / 2, 50, paint);
    }

结果如下:

First

但我希望文字有更高的高度,我想要这样的东西:

Second

我不想更改字体大小,只想更改此文本的高度。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

我找到了解决方法:

// Closing hardware acceleration
setLayerType(LAYER_TYPE_SOFTWARE, paint);

paint.getTextBounds(first, 0, first.length(), bounds);
float posX = (canvas.getWidth() - bounds.width()) / 2; // center
float posY = ((canvas.getHeight() - bounds.height()) / 2); // center

canvas.scale(1, 10, posX, posY);
canvas.drawText(first, posX, posY, paint);