private fun drawSprite(sprite: SVGADrawerSprite,canvas :Canvas) {
drawImage(sprite, canvas)
drawShape(sprite, canvas)
}
private fun drawImage(sprite: SVGADrawerSprite, canvas :Canvas) {
val imageKey = sprite.imageKey ?: return
dynamicItem.dynamicHidden[imageKey]?.takeIf { it }?.let { return }
(dynamicItem.dynamicImage[imageKey] ?: videoItem.images[imageKey])?.let {
resetShareMatrix(sprite.frameEntity.transform)
sharedPaint.reset()
sharedPaint.isAntiAlias = videoItem.antiAlias
sharedPaint.isFilterBitmap = videoItem.antiAlias
sharedPaint.alpha = (sprite.frameEntity.alpha * 255).toInt()
if (sprite.frameEntity.maskPath != null) {
val maskPath = sprite.frameEntity.maskPath ?: return@let
canvas.save()
sharedPath.reset()
maskPath.buildPath(sharedPath)
sharedPath.transform(sharedFrameMatrix)
canvas.clipPath(sharedPath)
sharedFrameMatrix.preScale((sprite.frameEntity.layout.width / it.width).toFloat(), (sprite.frameEntity.layout.width / it.width).toFloat())
canvas.drawBitmap(it, sharedFrameMatrix, sharedPaint)
canvas.restore()
}
else {
sharedFrameMatrix.preScale((sprite.frameEntity.layout.width / it.width).toFloat(), (sprite.frameEntity.layout.width / it.width).toFloat())
canvas.drawBitmap(it, sharedFrameMatrix, sharedPaint)
}
drawText(canvas,it, sprite)
}
}
private fun drawText(canvas :Canvas, drawingBitmap: Bitmap, sprite: SVGADrawerSprite) {
if (dynamicItem.isTextDirty) {
this.drawTextCache.clear()
dynamicItem.isTextDirty = false
}
val imageKey = sprite.imageKey ?: return
var textBitmap: Bitmap? = null
dynamicItem.dynamicText[imageKey]?.let { drawingText ->
dynamicItem.dynamicTextPaint[imageKey]?.let { drawingTextPaint ->
drawTextCache[imageKey]?.let {
textBitmap = it
} ?: kotlin.run {
textBitmap = Bitmap.createBitmap(drawingBitmap.width, drawingBitmap.height, Bitmap.Config.ARGB_8888)
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)
drawTextCache.put(imageKey, textBitmap as Bitmap)
}
}
}
dynamicItem.dynamicLayoutText[imageKey]?.let {
drawTextCache[imageKey]?.let {
textBitmap = it
} ?: kotlin.run {
it.paint.isAntiAlias = true
var layout = StaticLayout(it.text, 0, it.text.length, it.paint, drawingBitmap.width, it.alignment, it.spacingMultiplier, it.spacingAdd, false)
textBitmap = Bitmap.createBitmap(drawingBitmap.width, drawingBitmap.height, Bitmap.Config.ARGB_8888)
val textCanvas = Canvas(textBitmap)
textCanvas.translate(0f, ((drawingBitmap.height - layout.height) / 2).toFloat())
layout.draw(textCanvas)
drawTextCache.put(imageKey, textBitmap as Bitmap)
}
}
textBitmap?.let { textBitmap ->
sharedPaint.reset()
sharedPaint.isAntiAlias = videoItem.antiAlias
if (sprite.frameEntity.maskPath != null) {
val maskPath = sprite.frameEntity.maskPath ?: return@let
canvas.save()
canvas.concat(sharedFrameMatrix)
canvas.clipRect(0, 0, drawingBitmap.width, drawingBitmap.height)
val bitmapShader = BitmapShader(textBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT)
sharedPaint.shader = bitmapShader
sharedPath.reset()
maskPath.buildPath(sharedPath)
canvas.drawPath(sharedPath, sharedPaint)
canvas.restore()
}
else {
sharedPaint.isFilterBitmap = videoItem.antiAlias
canvas.drawBitmap(textBitmap, sharedFrameMatrix, sharedPaint)
}
}
}
private fun drawShape(sprite: SVGADrawerSprite, canvas :Canvas) {
resetShareMatrix(sprite.frameEntity.transform)
sprite.frameEntity.shapes.forEach { shape ->
shape.buildPath()
shape.shapePath?.let {
sharedPaint.reset()
sharedPaint.isAntiAlias = videoItem.antiAlias
sharedPaint.alpha = (sprite.frameEntity.alpha * 255).toInt()
if(!drawPathCache.containsKey(shape)){
sharedShapeMatrix.reset()
shape.transform?.let {
sharedShapeMatrix.postConcat(it)
}
sharedShapeMatrix.postConcat(sharedFrameMatrix)
val path = Path()
path.set(shape.shapePath)
path.transform(sharedShapeMatrix)
drawPathCache.put(shape,path)
}
shape.styles?.fill?.let {
if (it != 0x00000000) {
sharedPaint.color = it
if (sprite.frameEntity.maskPath !== null) canvas.save()
sprite.frameEntity.maskPath?.let { maskPath ->
sharedPath2.reset()
maskPath.buildPath(sharedPath2)
sharedPath2.transform(this.sharedFrameMatrix)
canvas.clipPath(sharedPath2)
}
canvas.drawPath(drawPathCache.get(shape), sharedPaint)
if (sprite.frameEntity.maskPath !== null) canvas.restore()
}
}
shape.styles?.strokeWidth?.let {
if (it > 0) {
resetShapeStrokePaint(shape)
if (sprite.frameEntity.maskPath !== null) canvas.save()
sprite.frameEntity.maskPath?.let { maskPath ->
sharedPath2.reset()
maskPath.buildPath(sharedPath2)
sharedPath2.transform(this.sharedFrameMatrix)
canvas.clipPath(sharedPath2)
}
canvas.drawPath(drawPathCache.get(shape), sharedPaint)
if (sprite.frameEntity.maskPath !== null) canvas.restore()
}
}
}
}
}