Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw text with different stroke and fill colors?

I want to display text as below in my app. I am using Paint class with style FILL_AND_STROKE to achieve this. But only one method setColor() is available to set the color.

How do I set different stroke and fill colors?

text with different stroke and fill colors

like image 953
Yugandhar Babu Avatar asked Jan 28 '12 11:01

Yugandhar Babu


2 Answers

Inside custom TextView (does not work in EditText):

@Override public void onDraw(Canvas canvas) {     final ColorStateList textColor = getTextColors();      TextPaint paint = this.getPaint();      paint.setStyle(Style.STROKE);     paint.setStrokeJoin(Join.ROUND);     paint.setStrokeMiter(10);     this.setTextColor(strokeColor);     paint.setStrokeWidth(strokeWidth);      super.onDraw(canvas);     paint.setStyle(Style.FILL);      setTextColor(textColor);     super.onDraw(canvas); } 
like image 163
Sergei S Avatar answered Sep 19 '22 16:09

Sergei S


Don't use FILL_AND_STROKE. Draw once with FILL and then change the color and draw with STROKE.

(That works for rectangles. I'm not sure STROKE works at all for text. You'll have to try it and find out.)

like image 42
Reuben Scratton Avatar answered Sep 23 '22 16:09

Reuben Scratton