Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you draw text with a border on a MapView in Android?

I'm trying to draw some text onto an MapView on Android. The drawing of the text goes fine, but it's very hard to read the text because it's white with no black border (like the rest of the text that appears naturally on MapViews to denote cities, states, and countries). I can't seem to figure how to draw the text with a black border. Anyone know how to do this?

This is the sort of code I'm using right now (this is just example code, found in one of my overlays):

@Override public void draw(Canvas canvas, MapView mapView, boolean shadow) {     Paint textPaint = new Paint();     textPaint.setARGB(255, 255, 255, 255);     textPaint.setTextAlign(Paint.Align.CENTER);     textPaint.setTextSize(16);     textPaint.setTypeface(Typeface.DEFAULT_BOLD);      canvas.drawText("Some Text", 100, 100, textPaint);      super.draw(canvas, mapView, shadow); } 
like image 733
Dan Lew Avatar asked Nov 12 '09 17:11

Dan Lew


1 Answers

The easiest way to do this is with a Stroke... something like this:

@Override public void draw(Canvas canvas, MapView mapView, boolean shadow) {     Paint strokePaint = new Paint();     strokePaint.setARGB(255, 0, 0, 0);     strokePaint.setTextAlign(Paint.Align.CENTER);     strokePaint.setTextSize(16);     strokePaint.setTypeface(Typeface.DEFAULT_BOLD);     strokePaint.setStyle(Paint.Style.STROKE);     strokePaint.setStrokeWidth(2);      Paint textPaint = new Paint();     textPaint.setARGB(255, 255, 255, 255);     textPaint.setTextAlign(Paint.Align.CENTER);     textPaint.setTextSize(16);     textPaint.setTypeface(Typeface.DEFAULT_BOLD);      canvas.drawText("Some Text", 100, 100, strokePaint);     canvas.drawText("Some Text", 100, 100, textPaint);      super.draw(canvas, mapView, shadow); } 

This will draw a border of 2 pixels around the outside of the text then draw the text over the top of it, giving you the illusion of an outline.

Also, it may be worth setting the Paints up in the constructor then just reusing them.

like image 101
Jeremy Logan Avatar answered Oct 08 '22 09:10

Jeremy Logan