问题
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?

回答1:
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);
}
回答2:
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.)
回答3:
not perfectly sure, but maybe you could use this:
link
TextView test = (TextView) findViewById(R.id.test);
test.setShadowLayer(float, float, float, int);
回答4:
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 12
mScorePaint.setColor(0xffff0000); // black
c.drawText(Integer.toString(mScore), x, y, mScorePaint); // red first
mScorePaint.setStrokeWidth(frameWidth/125.0f); // about 5
mScorePaint.setColor(0xff000000); // red
c.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.
来源:https://stackoverflow.com/questions/9044769/how-to-draw-text-with-different-stroke-and-fill-colors