Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit 1534cd2

Browse files
committed
fix: issue [#185](#185) drawTextOnBitmap 部分特殊文字会出现IndexOutOfBoundsException【必现.
1 parent 0133a0b commit 1534cd2

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

library/src/main/java/com/opensource/svgaplayer/drawer/SVGACanvasDrawer.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package com.opensource.svgaplayer.drawer
33
import android.annotation.TargetApi
44
import android.graphics.*
55
import android.os.Build
6-
import android.text.BoringLayout
7-
import android.text.StaticLayout
8-
import android.text.TextUtils
6+
import android.text.*
7+
import android.text.style.StyleSpan
98
import android.widget.FrameLayout
109
import android.widget.ImageView
1110
import com.opensource.svgaplayer.SVGADynamicEntity
@@ -245,15 +244,16 @@ internal class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVG
245244
textBitmap = it
246245
} ?: kotlin.run {
247246
textBitmap = Bitmap.createBitmap(drawingBitmap.width, drawingBitmap.height, Bitmap.Config.ARGB_8888)
247+
val drawRect = Rect(0, 0, drawingBitmap.width, drawingBitmap.height)
248248
val textCanvas = Canvas(textBitmap)
249249
drawingTextPaint.isAntiAlias = true
250-
val bounds = Rect()
251-
drawingTextPaint.getTextBounds(drawingText, 0, drawingText.length, bounds)
252-
val x = (drawingBitmap.width - bounds.width()) / 2.0
253-
val targetRectTop = 0
254-
val targetRectBottom = drawingBitmap.height
255-
val y = (targetRectBottom + targetRectTop - drawingTextPaint.fontMetrics.bottom - drawingTextPaint.fontMetrics.top) / 2
256-
textCanvas.drawText(drawingText, x.toFloat(), y, drawingTextPaint)
250+
drawingTextPaint.setStyle(Paint.Style.FILL);
251+
drawingTextPaint.setTextAlign(Paint.Align.CENTER);
252+
val fontMetrics = drawingTextPaint.getFontMetrics();
253+
val top = fontMetrics.top
254+
val bottom = fontMetrics.bottom
255+
val baseLineY = drawRect.centerY() - top/2 - bottom/2
256+
textCanvas.drawText(drawingText, drawRect.centerX().toFloat(),baseLineY,drawingTextPaint);
257257
drawTextCache.put(imageKey, textBitmap as Bitmap)
258258
}
259259
}

0 commit comments

Comments
 (0)