Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render crisp text using Canvas.drawText in Android

I'm doing an AppWidget and in its settings I'm letting the user enable/disable text shadow. Since I can't invoke the shadow method through the RemoteViews class, I'm doing a "draw" method that dynamically paints the widget and its container.

When drawing the text though, it gets kinda blurred and not that crisp like when using a TextView. The only code I've used for the text painting is:

Paint p = new Paint();
p.setAntiAlias(true);
p.setColor(Color.WHITE);

Are there any other magic I need to do for it to become more crisp?

like image 310
Tomas Avatar asked May 20 '10 12:05

Tomas


People also ask

How to draw text in canvas Android?

This article explains how to draw text inside a rectangle using the canvas in Android. Android Studio is used for the sample. In this, you will first create a paint object. Then call the setColor() method of paint to set the color of the rectangle by ing a color variable.

How to write text on canvas in android studio?

Another (arguably better) way to draw text on a canvas is to use a StaticLayout . This handles multiline text when needed. String text = "This is some text."; TextPaint textPaint = new TextPaint(); textPaint. setAntiAlias(true); textPaint.

How to draw bitmap on canvas in Android?

Use the Canvas method public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint) . Set dst to the size of the rectangle you want the entire image to be scaled into. EDIT: Here's a possible implementation for drawing the bitmaps in squares across on the canvas.

How is graphics drawn to canvas in Android development?

To draw onto a canvas in Android, you will need four things: A bitmap or a view — to hold the pixels where the canvas will be drawn. Canvas — to run the drawing commands on. Drawing commands — to indicate to the canvas what to draw.


2 Answers

Paint paint = new Paint(Paint.LINEAR_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

did the trick for me

like image 90
Tomas Avatar answered Oct 05 '22 00:10

Tomas


These are my text paint settings:

    textPaint = new Paint();
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setAntiAlias(true);
    textPaint.setARGB(255, 255, 255, 255);
    textPaint.setFakeBoldText(true);
    textPaint.setTextSize(textSize);

Seems to be working well for me.

like image 36
Abhinav Avatar answered Oct 04 '22 23:10

Abhinav