使用自定义高度在 Canvas 上绘制文本 - Android
Draw text on Canvas with custom Height - Android
我正在使用以下代码在 canvas 上绘制文本:
@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);
我正在使用以下代码在 canvas 上绘制文本:
@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);