如何绘制像BackgroundColorSpan这样的自定义背景?

时间:2015-01-07 08:14:36

标签: android spannablestring spannable

是否可以为文本背景绘制一个drawable(在BackgroundColorSpan中不是简单的纯色)?

例如圆形背景或Drawable形状。 也许我应该使用ReplacementSpan,但没有用。

public class RoundedBackgroundSpan extends ReplacementSpan
{
    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.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) {

        int color = paint.getColor();
        paint.setColor(Color.RED);
        canvas.drawRect(x, top, x+((int)paint.measureText(text, start, end)), bottom, paint);

        paint.setColor(color);
    }


}




SpannableString spannableString = new SpannableString("hello world!");
        spannableString.setSpan(new RoundedBackgroundSpan(), 0, 4 , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);


  textView.setText(spannableString);

0 个答案:

没有答案