android - ReplacementSpan 没有绘制一些空格字符
问题描述
我正在扩展 ReplacementSpan 以在 TextView 中的文本周围绘制一个框。在大多数情况下它工作正常,但有一个问题我无法解决:有时在行尾使用空格字符时不会绘制框。请参阅随附的屏幕截图。
这是我的跨度课程:
public class PlaceholderSpan extends ReplacementSpan implements
LineHeightSpan.WithDensity {
private final String TAG = PlaceholderSpan.class.getSimpleName();
int padding = 25;
int margin = 10;
int backgroundColor;
public PlaceholderSpan(int backgroundColor) {
super();
this.backgroundColor = backgroundColor;
}
@Override
public int getSize(@NonNull Paint paint, CharSequence charSequence, int start, int end, @Nullable Paint.FontMetricsInt fm) {
String textToDraw = charSequence.subSequence(start, end).toString();
int size;
if(textToDraw.equals(" ")) {
size = (int) paint.measureText("W", 0, 1) + padding + padding;
Log.d(TAG, "getSize: " + size);
} else {
size = (int) paint.measureText(charSequence, start, end) + padding + padding;
}
return size + margin + margin;
}
@Override
public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint p) {
String textToDraw = text.subSequence(start, end).toString();
int left= (int) x + margin;
int right = left + padding + (int) p.measureText(text, start, end) + padding;
p.setStyle(Paint.Style.STROKE);
p.setColor(backgroundColor);
Rect bounds = new Rect();
p.getTextBounds("W".toCharArray(), 0, 1 , bounds);
int height = top + bounds.height() + 35;
RectF rect = new RectF(left, top, right, height);
// draw the placeholder
canvas.drawRect(rect, p);
// draw the content
canvas.drawText(textToDraw, x + padding + margin, y, p);
if(textToDraw.equals(" ")) {
Log.d(TAG, "draw: " + textToDraw
+ ", start " + start
+ ", end " + end
+ ", x " + x
+ ", y " + y
+ ", bottom " + bottom
+ ", left " + left
+ ", right " + right
);
}
}
@Override
public void chooseHeight(CharSequence charSequence, int i, int i1, int i2, int i3, Paint.FontMetricsInt fontMetricsInt, TextPaint textPaint) {
Paint.FontMetricsInt fm = textPaint.getFontMetricsInt();
fontMetricsInt.top = fm.top;
fontMetricsInt.ascent = fm.ascent;
fontMetricsInt.descent = fm.descent;
// Our font now must accommodate the size of the drawable, so change the bottom of the
// font to accommodate the drawable.
fontMetricsInt.bottom = fm.bottom + 200 + margin;
fontMetricsInt.leading = fm.leading;
}
@Override
public void chooseHeight(CharSequence charSequence, int i, int i1, int i2, int i3, Paint.FontMetricsInt fm) {
}
}
这是我使用它的方式:
String str = "Very1 very2 .very3 very4 very5 very6 very7 very8 very9 very0 very1 very2 very3 very4 very5 very6 very7 very8 very9 very0";
Spannable spannable = new SpannableString(str);
for (int i = 1; i < str.length(); i++) {
if(str.substring(i-1, i).equals(" ")) {
spannable.setSpan(new PlaceholderSpan(Color.RED), i - 1, i, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
spannable.setSpan(new PlaceholderSpan(Color.BLUE), i - 1, i, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
textView.setText(spannable, TextView.BufferType.SPANNABLE);
每个单词后面必须有一个空格,但是如果它们位于行尾,则某些空格不会显示在框中。
如果有人能给我关于如何解决这个问题的提示,我将不胜感激
解决方案
推荐阅读
- matplotlib - 如何通过 matplotlib 在 spyder3 中绘图?
- java - 如何在java中将二进制有效负载(文件)转换为字节[]?
- ios - iOS 拖放。如何在拖动时用自定义视图替换单元格
- c++ - 编写 gtest 验证类是否为空
- javascript - Firebase 云消息传递 - JS SDK 中的 getToken() 错误
- android - Android Arcore Plane Tracker 目标
- python - 在 bash 和 python 之间保持 oracle DB 连接
- javascript - 计算 HTML5 画布中块箭头的点
- amazon-web-services - EC2 AWS 上的 Python Flask 应用程序,由 apache2 托管
- sql - Find by some columns with latest version