我想有一个自定义的跨度像下面的图片:

我想在一个不正确的单词下面创建一条Z字形的线。
我该怎么做呢?
发布于 2014-06-08 04:30:42
你肯定想对我拼凑出来的这个实现做一些检查。不过,还是这样。它希望它仍然能够为正确实现这样的功能提供一些基础。
实际的span类,令人惊讶的是,它不会替换任何东西。唯一的希望是,它真的可以绘制原始的跨度文本,因为给出了两行代码。加号还会画出“下划线”。
private class ErrorSpan extends ReplacementSpan {
private Paint errorPaint;
public ErrorSpan() {
errorPaint = new Paint();
errorPaint.setColor(Color.RED);
}
@Override
public int getSize(Paint paint, CharSequence text, int start, int end,
FontMetricsInt fm) {
return (int)paint.measureText(text, start, end);
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end,
float x, int top, int y, int bottom, Paint paint) {
// Render the red zigzag lines below text
float width = paint.measureText(text, start, end);
canvas.save();
canvas.clipRect(x, bottom - 5, x + width, bottom);
for (float lineX = x; lineX < x + width; lineX += 10) {
canvas.drawLine(lineX, bottom - 5, lineX + 5, bottom, errorPaint);
canvas.drawLine(lineX + 5, bottom, lineX + 10, bottom - 5, errorPaint);
}
canvas.restore();
// Render the span text as-is
canvas.drawText(text, start, end, x, y, paint);
}
};请原谅我在线条绘制循环中使用幻数(这很可能也会更有效)-但希望它能为最终创建产品质量实现提供足够好的基础。
用法大概是这样的:
TextView tv = (TextView)findViewById(R.id.textview);
Spannable spannable = Spannable.Factory.getInstance()
.newSpannable("testtest\ntesttest");
spannable.setSpan(new ErrorSpan(), 4, 8, 0);
spannable.setSpan(new ErrorSpan(), 9, 13, 0);
tv.setText(spannable);https://stackoverflow.com/questions/24100376
复制相似问题