Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw large sized text on a canvas?

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

Paint init:

   this.paint = new Paint();
   this.paint.setAntiAlias(true);
   this.paint.setDither(true);
   this.paint.setSubpixelText(true);
   this.paint.setColor(color_text_dark);
   this.paint.setTextAlign(Align.RIGHT);

Drawing:

   // Set the scale to the widest month
   float scale = getHeight() / this.max_month_width;
   String month_string = FULL_MONTH_NAME_FORMATTER.
                         format(active_month_calendar.getTime());
   canvas.save();
   canvas.translate(getWidth(), 0);
   canvas.rotate(-90);
   canvas.scale(scale, scale);
   canvas.drawText(month_string, 0, 0, this.paint);
   canvas.restore();

Result looks good on hdpi screen, but very ugly and pixelated on xhdpi one.

I did more test on various devices, and understood what result depends on Android version, not screen density and resolution.

Code works fine on 2.x platform, but doesn't work on 4.0.3+. Suppose, Android draw implementation was changed here. Full code you can see here.

hdpi version 2.3.5 (also tested 2.2)

hdpi

xhdpi version 4.2 (also tested 4.1, 4.0.3)

xhdpi

Trying different variations for paint antialias, subpixel text has no effect. How can I fix this issue?

like image 317
Sash0k Avatar asked Jan 26 '13 21:01

Sash0k


People also ask

How do you draw text on canvas?

To draw text on a canvas, the most important property and methods are: font - defines the font properties for the text. fillText(text,x,y) - draws "filled" text on the canvas. strokeText(text,x,y) - draws text on the canvas (no fill)

Which of the following method is use to write text on the canvas?

There are two methods fillText() and strokeText() to draw text on canvas.

What is TextPaint?

android.text.TextPaint. TextPaint is an extension of Paint that leaves room for some extra data used during text measuring and drawing.

How do you fill text in HTML?

The fillText() method draws filled text on the canvas. The default color of the text is black. Tip: Use the font property to specify font and font size, and use the fillStyle property to render the text in another color/gradient.


1 Answers

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

like image 196
Krylez Avatar answered Sep 21 '22 00:09

Krylez