43 lines
1.4 KiB
Java
43 lines
1.4 KiB
Java
|
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();
|
||
|
backgroundColor = context.getResources().getColor(R.color.colorAccent_full);
|
||
|
textColor = context.getResources().getColor(R.color.browser_actions_bg_grey);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NotNull Paint paint) {
|
||
|
RectF rect = new RectF(x, top, x + measureText(paint, text, start, end), bottom);
|
||
|
paint.setColor(backgroundColor);
|
||
|
int CORNER_RADIUS = 8;
|
||
|
canvas.drawRoundRect(rect, CORNER_RADIUS, CORNER_RADIUS, paint);
|
||
|
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);
|
||
|
}
|
||
|
}
|