我用这段代码在 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);
关于java - 使用自定义高度在 Canvas 上绘制文本 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29632228/