diff --git a/library/src/main/java/com/opensource/svgaplayer/drawer/SVGACanvasDrawer.kt b/library/src/main/java/com/opensource/svgaplayer/drawer/SVGACanvasDrawer.kt index 109e56b2..269bfafb 100644 --- a/library/src/main/java/com/opensource/svgaplayer/drawer/SVGACanvasDrawer.kt +++ b/library/src/main/java/com/opensource/svgaplayer/drawer/SVGACanvasDrawer.kt @@ -3,9 +3,8 @@ package com.opensource.svgaplayer.drawer import android.annotation.TargetApi import android.graphics.* import android.os.Build -import android.text.BoringLayout -import android.text.StaticLayout -import android.text.TextUtils +import android.text.* +import android.text.style.StyleSpan import android.widget.FrameLayout import android.widget.ImageView import com.opensource.svgaplayer.SVGADynamicEntity @@ -245,15 +244,16 @@ internal class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVG textBitmap = it } ?: kotlin.run { textBitmap = Bitmap.createBitmap(drawingBitmap.width, drawingBitmap.height, Bitmap.Config.ARGB_8888) + val drawRect = Rect(0, 0, drawingBitmap.width, drawingBitmap.height) val textCanvas = Canvas(textBitmap) drawingTextPaint.isAntiAlias = true - val bounds = Rect() - drawingTextPaint.getTextBounds(drawingText, 0, drawingText.length, bounds) - val x = (drawingBitmap.width - bounds.width()) / 2.0 - val targetRectTop = 0 - val targetRectBottom = drawingBitmap.height - val y = (targetRectBottom + targetRectTop - drawingTextPaint.fontMetrics.bottom - drawingTextPaint.fontMetrics.top) / 2 - textCanvas.drawText(drawingText, x.toFloat(), y, drawingTextPaint) + drawingTextPaint.setStyle(Paint.Style.FILL); + drawingTextPaint.setTextAlign(Paint.Align.CENTER); + val fontMetrics = drawingTextPaint.getFontMetrics(); + val top = fontMetrics.top + val bottom = fontMetrics.bottom + val baseLineY = drawRect.centerY() - top/2 - bottom/2 + textCanvas.drawText(drawingText, drawRect.centerX().toFloat(),baseLineY,drawingTextPaint); drawTextCache.put(imageKey, textBitmap as Bitmap) } }