3

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

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

这是结果:

第一的

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

第二

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

4

1 回答 1

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);
于 2015-04-24T11:57:53.023 回答