Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw with a default Android font on a Canvas?

I'm trying to draw a text on Canvas like this (kinda pseudocode below):

Paint p = new Paint(ANTI_ALIAS_FLAG);
p.setTextSize(18);
...
mCanvas.drawText("Hello StackOverflow!", 50, 50, p);

My problem is that the result looks really "weird". It uses some bold-like font, which is badly aliased, looks not pretty and "squarish".

I tried to play with p.setTextSize(), by setting various sizes, also I tried to set different default Typefaces by using p.setTypeface(Typeface) and setting DEFAULT, NORMAL, SERIF, SANS_SERIF etc, but it still looks ugly.

On the contrary the font used throughout the rest of the system looks really nice, and I'd like to use it.

How? :)

like image 809
dimsuz Avatar asked Nov 02 '10 08:11

dimsuz


People also ask

What font does canvas use by default?

The default font is 10px sans-serif.

What font does Android use by default?

Roboto is the default font on Android, and since 2013, other Google services such as Google Play, YouTube, Google Maps, and Google Images.

What is canvas drawing in Android?

Canvas is a class in Android that performs 2D drawing of different objects onto the screen. The saying “a blank canvas” is very similar to what a Canvas object is on Android. It is basically, an empty space to draw onto. The Canvas class is not a new concept, this class is actually wrapping a SKCanvas under the hood.


1 Answers

This will alias the font properly:

p.setAntiAlias(true);

If you want to change the font itself, then use

p.setTypeface(yourTypeface);

with a custom Typeface object.

(take a look at http://developer.android.com/reference/android/graphics/Typeface.html)

You can use

Typeface.defaultFromStyle(int style);

together with one of the text styles here: http://developer.android.com/reference/android/R.style.html#TextAppearance.

It should give you the default system font style, if that is what you're after.

like image 127
Joru Avatar answered Oct 10 '22 04:10

Joru