【问题标题】:Draw text on Canvas with custom Height - Android使用自定义高度在 Canvas 上绘制文本 - Android
【发布时间】:2015-06-20 08:26:11
【问题描述】:

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

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

结果如下:

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

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

【问题讨论】:

    标签: java android canvas drawing2d


    【解决方案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);
    

    【讨论】:

    • 能解释一下吗?
    • canvas.scale(1, 10, posX, posY);我只是将高度缩放 ​​10
    猜你喜欢
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多