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

    @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);
    }

结果如下:

但是我希望文本的高度更大,我想要这样的东西:

我不想更改字体大小,仅更改此文本的高度。我怎样才能做到这一点 ?

最佳答案

我找到了解决方案:

// 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);

10-08 10:52
查看更多