Skip to content

Instantly share code, notes, and snippets.

@TufferyJordan
Last active July 27, 2017 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TufferyJordan/b5ea9b75aefee66ca1e4fcf3e93bbbba to your computer and use it in GitHub Desktop.
Save TufferyJordan/b5ea9b75aefee66ca1e4fcf3e93bbbba to your computer and use it in GitHub Desktop.
This class is used to show Stroked Characters
public static class HollowSpan extends ReplacementSpan {
private final Path mPath = new Path();
private int mWidth;
private int mStrokeWidth;
private int mColorFill;
private int mColorStroke;
private PathEffect mPathEffect = null;
public HollowSpan(int strokeWidth, int strokeColor) {
mColorStroke = strokeColor;
mStrokeWidth = strokeWidth;
}
public void setPathEffect(PathEffect effect) {
mPathEffect = effect;
}
public int getSize(Paint paint, CharSequence text,
int start, int end, Paint.FontMetricsInt fm) {
mWidth = (int) (paint.measureText(text, start, end) +
mStrokeWidth);
return mWidth;
}
public void draw(
Canvas canvas, CharSequence text, int start, int end,
float x, int top, int y, int bottom, Paint paint) {
paint.setPathEffect(mPathEffect);
paint.setStrokeWidth(mStrokeWidth);
mColorFill = paint.getColor();
mPath.reset();
paint.getTextPath(text.toString(), start, end, x, y, mPath);
mPath.close();
canvas.translate(paint.getStrokeWidth() / 2, 0);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(mColorStroke);
canvas.drawPath(mPath, paint);
paint.setStyle(Paint.Style.FILL);
paint.setColor(mColorFill);
canvas.drawPath(mPath, paint);
canvas.translate(-paint.getStrokeWidth() / 2, 0);
}
public static CharSequence addStroke(CharSequence charSequence) {
final HollowSpan span = new HollowSpan(4, Color.BLACK);
final SpannableString spannableString = new SpannableString(charSequence);
spannableString.setSpan(span, 0, charSequence.length(), 0);
return spannableString;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment