How to draw large sized text on a canvas?

前端 未结 2 1362
时光取名叫无心
时光取名叫无心 2020-12-10 08:00

I want to draw on canvas month\'s text vertical along screen height.

Paint init:

   this.paint = new Paint();
         


        
2条回答
  •  再見小時候
    2020-12-10 08:36

    The problem is that you're drawing text at one size and scaling the result up. Once you've determined how wide you want the text to be, you should use calls to Paint.measureText(), adjusting the size via Paint.setTextSize() accordingly. Once it measures correctly, then you do your call to Canvas.drawText().

    An alternative would be to not measure the text at all and just immediately call:

    paint.setTextSize(paint.getSize() * scale)
    

    There's no guarantee the text will fit in this case, though.

    None of your other transform calls should result in interpolation, so it should give you very sharp lines.

    Edit

    Here is a code sample and comparison screenshot:

    canvas.save();
    canvas.scale(10, 10);
    canvas.drawText("Hello", 0, 10, mTextPaint);
    canvas.restore();
    float textSize = mTextPaint.getTextSize();
    mTextPaint.setTextSize(textSize * 10);
    canvas.drawText("Hello", 0, 300, mTextPaint);
    mTextPaint.setTextSize(textSize);
    

    Your rendering method on top, mine on bottom

提交回复
热议问题