Skip to content

Commit b7555c3

Browse files
committed
reuse drawer.
bug-fix: Shape strokeWidth issue.
1 parent cb33d6f commit b7555c3

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

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

+66-6
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import android.widget.ImageView
88
* Created by cuiminghui on 2017/3/29.
99
*/
1010

11-
class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicEntity, private val canvas: Canvas) : SGVADrawer(videoItem) {
11+
class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicEntity) : SGVADrawer(videoItem) {
1212

13+
var canvas: Canvas? = null
14+
private var ratio = 1.0f
15+
private var ratioX = false
1316
private val sharedPaint = Paint()
1417
private val sharedPath = Path()
1518
private val sharedPath2 = Path()
@@ -24,6 +27,7 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
2427
}
2528

2629
private fun performScaleType(scaleType: ImageView.ScaleType) {
30+
val canvas = this.canvas ?: return
2731
if (canvas.width == 0 || canvas.height == 0 || videoItem.videoSize.width == 0.0 || videoItem.videoSize.height == 0.0) {
2832
return
2933
}
@@ -35,10 +39,14 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
3539
val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
3640
val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
3741
if (videoRatio > canvasRatio) {
42+
ratio = (canvas.height / videoItem.videoSize.height).toFloat()
43+
ratioX = false
3844
sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
3945
sharedContentTransform.postTranslate(((canvas.width - videoItem.videoSize.width * (canvas.height / videoItem.videoSize.height)) / 2.0).toFloat(), 0.0f)
4046
}
4147
else {
48+
ratio = (canvas.width / videoItem.videoSize.width).toFloat()
49+
ratioX = true
4250
sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
4351
sharedContentTransform.postTranslate(0.0f, ((canvas.height - videoItem.videoSize.height * (canvas.width / videoItem.videoSize.width)) / 2.0).toFloat())
4452
}
@@ -51,10 +59,14 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
5159
val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
5260
val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
5361
if (videoRatio > canvasRatio) {
62+
ratio = (canvas.width / videoItem.videoSize.width).toFloat()
63+
ratioX = true
5464
sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
5565
sharedContentTransform.postTranslate(0.0f, ((canvas.height - videoItem.videoSize.height * (canvas.width / videoItem.videoSize.width)) / 2.0).toFloat())
5666
}
5767
else {
68+
ratio = (canvas.height / videoItem.videoSize.height).toFloat()
69+
ratioX = false
5870
sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
5971
sharedContentTransform.postTranslate(((canvas.width - videoItem.videoSize.width * (canvas.height / videoItem.videoSize.height)) / 2.0).toFloat(), 0.0f)
6072
}
@@ -64,10 +76,14 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
6476
val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
6577
val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
6678
if (videoRatio > canvasRatio) {
79+
ratio = (canvas.width / videoItem.videoSize.width).toFloat()
80+
ratioX = true
6781
sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
6882
sharedContentTransform.postTranslate(0.0f, ((canvas.height - videoItem.videoSize.height * (canvas.width / videoItem.videoSize.width)) / 2.0).toFloat())
6983
}
7084
else {
85+
ratio = (canvas.height / videoItem.videoSize.height).toFloat()
86+
ratioX = false
7187
sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
7288
sharedContentTransform.postTranslate(((canvas.width - videoItem.videoSize.width * (canvas.height / videoItem.videoSize.height)) / 2.0).toFloat(), 0.0f)
7389
}
@@ -76,28 +92,40 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
7692
val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
7793
val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
7894
if (videoRatio > canvasRatio) {
95+
ratio = (canvas.width / videoItem.videoSize.width).toFloat()
96+
ratioX = true
7997
sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
8098
}
8199
else {
100+
ratio = (canvas.height / videoItem.videoSize.height).toFloat()
101+
ratioX = false
82102
sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
83103
}
84104
}
85105
ImageView.ScaleType.FIT_END -> {
86106
val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
87107
val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
88108
if (videoRatio > canvasRatio) {
109+
ratio = (canvas.width / videoItem.videoSize.width).toFloat()
110+
ratioX = true
89111
sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
90112
sharedContentTransform.postTranslate(0.0f, (canvas.height - videoItem.videoSize.height * (canvas.width / videoItem.videoSize.width)).toFloat())
91113
}
92114
else {
115+
ratio = (canvas.height / videoItem.videoSize.height).toFloat()
116+
ratioX = false
93117
sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
94118
sharedContentTransform.postTranslate((canvas.width - videoItem.videoSize.width * (canvas.height / videoItem.videoSize.height)).toFloat(), 0.0f)
95119
}
96120
}
97121
ImageView.ScaleType.FIT_XY -> {
122+
ratio = Math.max((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
123+
ratioX = (canvas.width / videoItem.videoSize.width).toFloat() > (canvas.height / videoItem.videoSize.height).toFloat()
98124
sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
99125
}
100126
else -> {
127+
ratio = (canvas.width / videoItem.videoSize.width).toFloat()
128+
ratioX = true
101129
sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
102130
}
103131
}
@@ -109,6 +137,7 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
109137
}
110138

111139
private fun drawImage(sprite: SVGADrawerSprite, scaleType: ImageView.ScaleType) {
140+
val canvas = this.canvas ?: return
112141
(dynamicItem.dynamicImage[sprite.imageKey] ?: videoItem.images[sprite.imageKey])?.let {
113142
sharedPaint.reset()
114143
sharedContentTransform.reset()
@@ -135,6 +164,7 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
135164
}
136165

137166
private fun drawText(drawingBitmap: Bitmap, sprite: SVGADrawerSprite) {
167+
val canvas = this.canvas ?: return
138168
dynamicItem.dynamicText[sprite.imageKey]?.let { drawingText ->
139169
dynamicItem.dynamicTextPaint[sprite.imageKey]?.let { drawingTextPaint ->
140170
val textBitmap = Bitmap.createBitmap(drawingBitmap.width, drawingBitmap.height, Bitmap.Config.ARGB_8888)
@@ -168,6 +198,7 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
168198
}
169199

170200
private fun drawShape(sprite: SVGADrawerSprite, scaleType: ImageView.ScaleType) {
201+
val canvas = this.canvas ?: return
171202
sharedContentTransform.reset()
172203
performScaleType(scaleType)
173204
sharedContentTransform.preConcat(sprite.frameEntity.transform)
@@ -221,15 +252,44 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
221252
}
222253
}
223254

255+
private val tValues = FloatArray(16)
256+
257+
private fun requestScale(): Float {
258+
this.sharedContentTransform.getValues(tValues)
259+
if (tValues[0] == 0f) {
260+
return 0f
261+
}
262+
var A = tValues[0].toDouble()
263+
var B = tValues[3].toDouble()
264+
var C = tValues[1].toDouble()
265+
var D = tValues[4].toDouble()
266+
if (A * D == B * C) return 0f
267+
var scaleX = Math.sqrt(A * A + B * B)
268+
A /= scaleX
269+
B /= scaleX
270+
var skew = A * C + B * D
271+
C -= A * skew
272+
D -= B * skew
273+
var scaleY = Math.sqrt(C * C + D * D)
274+
C /= scaleY
275+
D /= scaleY
276+
skew /= scaleY
277+
if ( A * D < B * C ) {
278+
scaleX = -scaleX
279+
}
280+
return if (this.ratioX) ratio / Math.abs(scaleX.toFloat()) else ratio / Math.abs(scaleY.toFloat())
281+
}
282+
224283
private fun resetShapeStrokePaint(shape: SVGAVideoShapeEntity) {
284+
225285
sharedPaint.reset()
226286
sharedPaint.isAntiAlias = true
227287
sharedPaint.style = Paint.Style.STROKE
228288
shape.styles?.stroke?.let {
229289
sharedPaint.color = it
230290
}
231291
shape.styles?.strokeWidth?.let {
232-
sharedPaint.strokeWidth = it
292+
sharedPaint.strokeWidth = it * requestScale()
233293
}
234294
shape.styles?.lineCap?.let {
235295
when {
@@ -246,14 +306,14 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
246306
}
247307
}
248308
shape.styles?.miterLimit?.let {
249-
sharedPaint.strokeMiter = it.toFloat()
309+
sharedPaint.strokeMiter = it.toFloat() * requestScale()
250310
}
251311
shape.styles?.lineDash?.let {
252312
if (it.size == 3) {
253313
sharedPaint.pathEffect = DashPathEffect(floatArrayOf(
254-
(if (it[0] < 1.0f) 1.0f else it[0]),
255-
(if (it[1] < 0.1f) 0.1f else it[1])
256-
), it[2])
314+
(if (it[0] < 1.0f) 1.0f else it[0]) * requestScale(),
315+
(if (it[1] < 0.1f) 0.1f else it[1]) * requestScale()
316+
), it[2] * requestScale())
257317
}
258318
}
259319
}

library/src/main/java/com/opensource/svgaplayer/SVGAImageView.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ class SVGADrawable(val videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
4646

4747
var scaleType: ImageView.ScaleType = ImageView.ScaleType.MATRIX
4848

49+
internal val drawer = SVGACanvasDrawer(videoItem, dynamicItem)
50+
4951
override fun draw(canvas: Canvas?) {
5052
if (cleared) {
5153
return
5254
}
5355
canvas?.let {
54-
val drawer = SVGACanvasDrawer(videoItem, dynamicItem, it)
56+
drawer.canvas = it
5557
drawer.drawFrame(currentFrame, scaleType)
5658
}
5759
}

0 commit comments

Comments
 (0)