2020-09-16 19:02:04 +02:00
|
|
|
package app.fedilab.fedilabtube.helper;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Paint;
|
|
|
|
import android.graphics.RectF;
|
|
|
|
import android.text.style.ReplacementSpan;
|
|
|
|
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
import app.fedilab.fedilabtube.R;
|
|
|
|
|
|
|
|
public class RoundedBackgroundSpan extends ReplacementSpan {
|
|
|
|
|
|
|
|
private int backgroundColor;
|
|
|
|
private int textColor;
|
|
|
|
|
|
|
|
public RoundedBackgroundSpan(Context context) {
|
|
|
|
super();
|
2020-09-18 12:03:35 +02:00
|
|
|
backgroundColor = context.getResources().getColor(R.color.tag_color);
|
|
|
|
textColor = context.getResources().getColor(R.color.tag_color_text);
|
2020-09-16 19:02:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NotNull Paint paint) {
|
2020-09-25 18:58:04 +02:00
|
|
|
RectF rect = new RectF(x, top + 2, x + measureText(paint, text, start, end), bottom - 1);
|
2020-09-16 19:02:04 +02:00
|
|
|
paint.setColor(backgroundColor);
|
2020-09-17 17:00:12 +02:00
|
|
|
canvas.drawRoundRect(rect, 8, 8, paint);
|
2020-09-16 19:02:04 +02:00
|
|
|
paint.setColor(textColor);
|
|
|
|
canvas.drawText(text, start, end, x, y, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
|
|
|
|
return Math.round(paint.measureText(text, start, end));
|
|
|
|
}
|
|
|
|
|
|
|
|
private float measureText(Paint paint, CharSequence text, int start, int end) {
|
|
|
|
return paint.measureText(text, start, end);
|
|
|
|
}
|
|
|
|
}
|