我浏览了大纲文本的实际库。这里:https ://github.com/santaevpavel/OutlineSpan
我发现了一个新的更新版本OutlineSpan
。您可以使用以下内容替换您的版本。
class OutlineSpan(
@ColorInt private val strokeColor: Int,
@Dimension private val strokeWidth: Float
): ReplacementSpan() {
override fun getSize(
paint: Paint,
text: CharSequence,
start: Int,
end: Int,
fontMetrics: Paint.FontMetricsInt?
): Int {
if (fontMetrics != null && paint.fontMetricsInt != null) {
fontMetrics.bottom = paint.fontMetricsInt.bottom
fontMetrics.top = paint.fontMetricsInt.top
fontMetrics.descent = paint.fontMetricsInt.descent
fontMetrics.leading = paint.fontMetricsInt.leading
}
return paint.measureText(text.toString().substring(start until end)).toInt()
}
override fun draw(
canvas: Canvas,
text: CharSequence,
start: Int,
end: Int,
x: Float,
top: Int,
y: Int,
bottom: Int,
paint: Paint
) {
val originTextColor = paint.color
paint.apply {
color = strokeColor
style = Paint.Style.STROKE
this.strokeWidth = this@OutlineSpan.strokeWidth
}
canvas.drawText(text, start, end, x, y.toFloat(), paint)
paint.apply {
color = originTextColor
style = Paint.Style.FILL
}
canvas.drawText(text, start, end, x, y.toFloat(), paint)
}
}
然后像这样使用它:
val string = "f"
val spannableString = SpannableString(string)
val outlineSpan = OutlineSpan(ContextCompat.getColor(this, R.color.colorAccent), 3f)
spannableString.setSpan(outlineSpan, 0, 1, 0)
textViewHollow.text = spannableString
这解决了这个问题。
输出: