提问者:小点点

如何在Android中用canvas.drawtext绘制跨越字符串


我想将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信息(这里特别是前景和背景色)?

相关的

  • 如何在Android中遍历SpannedString或SpannableString中的跨
  • 是否可以通过一次调用canvas.drawtext()来显示多种颜色的文本?

以下是我到目前为止的作品:

  • 将每个跨度范围绘制为单独的文本运行
  • 使用drawtextRun绘制文本(示例)(更新:直到API 23才添加)
  • 使用GetRunAdvance测量从哪里开始下一次文本运行(更新:直到API 23才添加,请使用MeasureText)
  • 背景色可能需要单独绘制(使用drawrectdrawpath?请参见此处、此处和此处。)
  • TextViewStatiClayoutTextLine
  • 的源代码

共1个答案

匿名用户

对于大多数遇到这个问题的人来说,您可能应该使用staticlayout来绘制跨越文本。请参见此答案以获得帮助。

但是,如果您确实需要自己绘制跨越的文本,那么您将需要遍历所有跨越的范围,并分别绘制每个范围。您还需要测量每个跨度中文本的长度,以便知道从哪里开始绘制下一个跨度。

下面的代码处理backgroundcolorspanforregroundcolorspan

// 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;
}