1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| import android.content.Context import android.graphics.Canvas import android.graphics.Paint import android.graphics.drawable.Drawable import android.text.style.ImageSpan
open class MarginImageSpan( context: Context, drawableRes: Int, val l: Int = 0, val r: Int = 0 ) : ImageSpan(context, drawableRes) {
override fun getSize( paint: Paint, text: CharSequence?, start: Int, end: Int, fm: Paint.FontMetricsInt? ): Int { val d: Drawable = drawable val rect = d.bounds
if (fm != null) { val var7 = Paint.FontMetricsInt() paint.getFontMetricsInt(var7) fm.ascent = var7.ascent fm.descent = var7.descent fm.top = var7.top fm.bottom = var7.bottom }
return l + rect.right + r }
override fun draw( canvas: Canvas, text: CharSequence, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint ) { val b = drawable val fm = paint.fontMetricsInt var transY = ((y + fm.descent + y + fm.ascent) / 2 - b.bounds.bottom / 2) if (transY > bottom - b.bounds.bottom) transY = bottom - b.bounds.bottom val transX = x + l canvas.save() canvas.translate(transX, transY.toFloat()) b.draw(canvas) canvas.restore() } }
|