// ReplacementSpan does not support Spans that span multiple lines in a TextView! // Hack: Limit label length to 16 chars privatestaticfinalint LABEL_MAX_LENGTH = 16;
public MentionSpan(
@ColorInt int backgroundColor,
@ColorInt int invertedColor,
@ColorInt int textColor,
@ColorInt int invertedTextColor, boolean strikeThroughText
) { super();
@Override publicint getSize(@NonNull Paint paint, CharSequence text, int start, int end, @Nullable Paint.FontMetricsInt fm) { if (!TestUtil.isBlankOrNull(text) && end - start == 11) {
String labelText = getMentionLabelText(text, start, end); if (!TestUtil.isEmptyOrNull(labelText)) { if (fm != null) { // The span is not rendered if it spans the entire text and no height is set. Therefore, we need to apply the font metrics // here such that a height can be calculated. final Paint.FontMetrics fontMetrics = paint.getFontMetrics();
fm.ascent = (int) fontMetrics.ascent;
fm.bottom = (int) fontMetrics.bottom;
fm.descent = (int) fontMetrics.descent;
fm.leading = (int) fontMetrics.leading;
fm.top = (int) fontMetrics.top;
}
@Override publicvoid draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) { if (width != 0 && !TestUtil.isBlankOrNull(text) && end - start == 11) { int alpha = paint.getAlpha();
String identity = text.subSequence(start + 2, end - 1).toString();
if (identity.equals(ContactService.ALL_USERS_PLACEHOLDER_ID) || identity.equals(userService.getIdentity())) {
canvas.drawRoundRect(new RectF(x, top + 1f, x + width, bottom), RADIUS, RADIUS, invertedPaint);
paint.setColor(this.invertedTextColor);
paint.setAlpha(0x78);
} else {
canvas.drawRoundRect(new RectF(x, top + 1f, x + width, bottom), RADIUS, RADIUS, backgroundPaint);
paint.setColor(this.textColor);
paint.setAlpha(0x50);
}
canvas.drawText(MENTION_INDICATOR, x + PADDING, y, paint);
paint.setAlpha(0xFF);
canvas.drawText(getMentionLabelText(text, start, end), x + PADDING + paint.measureText(MENTION_INDICATOR), y, paint);
paint.setAlpha(alpha);
if (strikeThroughText) {
setStrikeThroughText(canvas, paint, x, y);
}
}
}
privatevoid setStrikeThroughText(@NonNull Canvas canvas, @NonNull Paint paint, float x, float y) { // Draw an empty string that covers the canvas with strike through flag boolean isStrikeThruText = paint.isStrikeThruText();
paint.setStrikeThruText(true);
// Create empty string that covers the whole canvas width (font dependent) int numSpaces = (int) Math.ceil(width / paint.measureText(" "));
StringBuilder spaces = new StringBuilder(); for (int i = 0; i < numSpaces; i++) {
spaces.append(" ");
}
canvas.drawText(spaces.toString(), x, y, paint);
// Restore strike through state to prevent side effects
paint.setStrikeThruText(isStrikeThruText);
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-04-27)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.