How to draw text with different stroke and fill colors? How to draw text with different stroke and fill colors? android android

How to draw text with different stroke and fill colors?


Inside custom TextView (does not work in EditText):

@Overridepublic 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);}


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.)


I used the first solution above to come up with this idea: put down a larger STROKE, text and then overlay it with a smaller FILL_AND_STROKE text:

mScorePaint = new TextPaint();mScorePaint.setTextSize(63);mScorePaint.setStyle(Style.STROKE);mScorePaint.setStrokeJoin(Join.ROUND);mScorePaint.setStrokeMiter(10.0f);mScorePaint.setStrokeWidth(frameWidth/50.0f); // about 12mScorePaint.setColor(0xffff0000); // blackc.drawText(Integer.toString(mScore), x, y, mScorePaint);  // red firstmScorePaint.setStrokeWidth(frameWidth/125.0f); // about 5mScorePaint.setColor(0xff000000); // redc.drawText(Integer.toString(mScore), x, y, mScorePaint);  // black on top

Because the FILL alone was not seeing any of the Stroke attributes and was coming out very thin.