我想将SpannedString
绘制到canvas
。
SpannableString spannableString = new SpannableString("Hello World!");
ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED);
BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.YELLOW);
spannableString.setSpan(foregroundSpan, 1, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(backgroundSpan, 3, spannableString.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
上面的示例是使用TextView
绘制的,后者又使用布局
绘制带有跨距的文本。我知道使用布局
是将文本绘制到画布的推荐方法。但是,我正在从头开始制作自己的文本布局,所以我需要自己实现这个。
这样做是行不通的
canvas.drawText(spannableString, 0, spannableString.length(), 0, 0, mTextPaint);
如何使用canvas.drawtext
(或drawtextRun
)绘制span信息(这里特别是前景和背景色)?
相关的
以下是我到目前为止的作品:
drawtextRun
绘制文本(示例)(更新:直到API 23才添加)GetRunAdvance
测量从哪里开始下一次文本运行(更新:直到API 23才添加,请使用MeasureText
)drawrect
或drawpath
?请参见此处、此处和此处。)TextView
、StatiClayout
和TextLine
对于大多数遇到这个问题的人来说,您可能应该使用staticlayout
来绘制跨越文本。请参见此答案以获得帮助。
但是,如果您确实需要自己绘制跨越的文本,那么您将需要遍历所有跨越的范围,并分别绘制每个范围。您还需要测量每个跨度中文本的长度,以便知道从哪里开始绘制下一个跨度。
下面的代码处理backgroundcolorspan
和forregroundcolorspan
。
// set up the spanned string
SpannableString spannableString = new SpannableString("Hello World!");
ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED);
BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.YELLOW);
spannableString.setSpan(foregroundSpan, 1, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(backgroundSpan, 3, spannableString.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// draw each span one at a time
int next;
float xStart = 0;
float xEnd;
for (int i = 0; i < spannableString.length(); i = next) {
// find the next span transition
next = spannableString.nextSpanTransition(i, spannableString.length(), CharacterStyle.class);
// measure the length of the span
xEnd = xStart + mTextPaint.measureText(spannableString, i, next);
// draw the highlight (background color) first
BackgroundColorSpan[] bgSpans = spannableString.getSpans(i, next, BackgroundColorSpan.class);
if (bgSpans.length > 0) {
mHighlightPaint.setColor(bgSpans[0].getBackgroundColor());
canvas.drawRect(xStart, mTextPaint.getFontMetrics().top, xEnd, mTextPaint.getFontMetrics().bottom, mHighlightPaint);
}
// draw the text with an optional foreground color
ForegroundColorSpan[] fgSpans = spannableString.getSpans(i, next, ForegroundColorSpan.class);
if (fgSpans.length > 0) {
int saveColor = mTextPaint.getColor();
mTextPaint.setColor(fgSpans[0].getForegroundColor());
canvas.drawText(spannableString, i, next, xStart, 0, mTextPaint);
mTextPaint.setColor(saveColor);
} else {
canvas.drawText(spannableString, i, next, xStart, 0, mTextPaint);
}
xStart = xEnd;
}