Skip to content

Instantly share code, notes, and snippets.

@efemoney
Last active December 5, 2017 14:29
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 efemoney/1262972c5e5bfd59d63598761d01badc to your computer and use it in GitHub Desktop.
Save efemoney/1262972c5e5bfd59d63598761d01badc to your computer and use it in GitHub Desktop.
public class HorizontalLineSpan extends ReplacementSpan {
private Context context;
public HorizontalLineSpan(Context context) {
this.context = context;
}
@Override
public int getSize(Paint paint, CharSequence text,
int start, int end,
Paint.FontMetricsInt fm) {
return 0;
}
@Override
public void draw(Canvas canvas, CharSequence text,
int start, int end, float x,
int top, int y, int bottom, Paint paint) {
Paint.FontMetricsInt fmi = paint.getFontMetricsInt();
int height = fmi.descent - fmi.ascent; // set the drawable height to the height of the current font
Drawable b = getDrawable(canvas.getWidth(), height);
canvas.save();
int transY = bottom - b.getBounds().bottom;
canvas.translate(x, transY);
b.draw(canvas);
canvas.restore();
}
private Drawable getDrawable(int width, int height) {
// We copy the implementation of getCachedDrawable in DynamicDrawableSpan
Drawable d = getCachedDrawable();
d.setBounds(0, 0, width, height);
return d;
}
public Drawable getDrawable() {
return new LineDrawable(context);
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment