40 lines
1.5 KiB
Kotlin
40 lines
1.5 KiB
Kotlin
package com.adlerosn.brasilfurfest.helper
|
|
|
|
import android.graphics.Canvas
|
|
import android.graphics.Paint
|
|
import android.graphics.RectF
|
|
import android.text.style.ReplacementSpan
|
|
import kotlin.math.roundToInt
|
|
|
|
|
|
class RoundedBackgroundAndForegroundColorsSpan(
|
|
val backgroundColor: Int,
|
|
val foregroundColor: Int,
|
|
radius: Int = 3,
|
|
paddingX: Int = 4,
|
|
paddingY: Int = 2
|
|
): ReplacementSpan() {
|
|
private val radius = radius.asDpToPx
|
|
|
|
private val paddingX = paddingX.asDpToPx
|
|
private val paddingY = paddingY.asDpToPx
|
|
|
|
override fun draw(canvas: Canvas, text: CharSequence, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paintArg: Paint) {
|
|
val paint = Paint(paintArg)
|
|
|
|
paint.color = backgroundColor
|
|
val tagBottom = top.toFloat() + paddingY + paint.textSize + paddingY
|
|
val tagRight = x + getTagWidth(text, start, end, paint)
|
|
val rect = RectF(x, top.toFloat(), tagRight, tagBottom)
|
|
canvas.drawRoundRect(rect, radius, radius, paint)
|
|
|
|
paint.color = foregroundColor
|
|
canvas.drawText(text, start, end, x + paddingX, tagBottom - 2f*paddingY, paint)
|
|
}
|
|
|
|
private fun getTagWidth(text: CharSequence, start: Int, end: Int, paint: Paint): Int =
|
|
(paddingX + paint.measureText(text.subSequence(start, end).toString()) + paddingX).roundToInt()
|
|
|
|
override fun getSize(paint: Paint, text: CharSequence, start: Int, end: Int, fm: Paint.FontMetricsInt?): Int =
|
|
getTagWidth(text, start, end, Paint(paint))
|
|
} |