Draw shadowed text into a canvas

Quickly share your Android Code Snippets here...

Draw shadowed text into a canvas

Postby B_Thax » Mon Aug 09, 2010 7:59 pm

Some times when you need to draw some simple text into the canvas you need to draw it with a surrounding shadow to get some contrast and to be able to read the text if the background has been drawn with a similar color. This is not the most efficient way to do it but it's quite simple.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. /**
  2.  * Draw specified text with a fully sourrounding shadow.
  3.  *
  4.  * @param canvas - Canvas we will draw to
  5.  * @param text - Text we will draw
  6.  * @param x - x position
  7.  * @param y - y position
  8.  * @param textPaint - Paint object used to draw the actual text
  9.  * @param shadowPaint - Paint object used to draw the background shadow
  10.  */
  11. public static void drawTextShadowed(Canvas canvas, String text, float x, float y, Paint textPaint, Paint shadowPaint) {
  12.     // Draw all 4 sides of the shadow
  13.     canvas.drawText(text, x-1, y, shadowPaint);
  14.     canvas.drawText(text, x, y+1, shadowPaint);
  15.     canvas.drawText(text, x+1, y, shadowPaint);
  16.     canvas.drawText(text, x, y-1, shadowPaint);
  17.    
  18.     // Draw the final overlay text in the middle
  19.     canvas.drawText(text, x, y, textPaint);
  20. }
  21.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Cheers,
Moss
Game Programmer at Tragnarion Studios - www.Tragnarion.com
User avatar
B_Thax
Junior Developer
Junior Developer
 
Posts: 13
Joined: Thu Jul 17, 2008 5:55 pm
Location: Palma de Mallorca

Top

Re: Draw shadowed text into a canvas

Postby pskink » Tue Aug 10, 2010 8:02 am

B_Thax wrote:Some times when you need to draw some simple text into the canvas you need to draw it with a surrounding shadow to get some contrast and to be able to read the text if the background has been drawn with a similar color. This is not the most efficient way to do it but it's quite simple.

Cheers,
Moss


you can use http://developer.android.com/reference/ ... ayer(float, float, float, int)



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. textPaint.setShadowLayer(radius, dx, dy, color);
  2. canvas.drawText(text, x, y, textPaint);
  3.  
  4.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 4 guests