@@ -8,8 +8,11 @@ import android.widget.ImageView
8
8
* Created by cuiminghui on 2017/3/29.
9
9
*/
10
10
11
- class SVGACanvasDrawer (videoItem : SVGAVideoEntity , val dynamicItem : SVGADynamicEntity , private val canvas : Canvas ) : SGVADrawer(videoItem) {
11
+ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicEntity) : SGVADrawer(videoItem) {
12
12
13
+ var canvas: Canvas? = null
14
+ private var ratio = 1.0f
15
+ private var ratioX = false
13
16
private val sharedPaint = Paint()
14
17
private val sharedPath = Path()
15
18
private val sharedPath2 = Path()
@@ -24,6 +27,7 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
24
27
}
25
28
26
29
private fun performScaleType(scaleType: ImageView.ScaleType) {
30
+ val canvas = this.canvas ?: return
27
31
if (canvas.width == 0 || canvas.height == 0 || videoItem.videoSize.width == 0.0 || videoItem.videoSize.height == 0.0) {
28
32
return
29
33
}
@@ -35,10 +39,14 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
35
39
val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
36
40
val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
37
41
if (videoRatio > canvasRatio) {
42
+ ratio = (canvas.height / videoItem.videoSize.height).toFloat()
43
+ ratioX = false
38
44
sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
39
45
sharedContentTransform.postTranslate(((canvas.width - videoItem.videoSize.width * (canvas.height / videoItem.videoSize.height)) / 2.0).toFloat(), 0.0f)
40
46
}
41
47
else {
48
+ ratio = (canvas.width / videoItem.videoSize.width).toFloat()
49
+ ratioX = true
42
50
sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
43
51
sharedContentTransform.postTranslate(0.0f, ((canvas.height - videoItem.videoSize.height * (canvas.width / videoItem.videoSize.width)) / 2.0).toFloat())
44
52
}
@@ -51,10 +59,14 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
51
59
val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
52
60
val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
53
61
if (videoRatio > canvasRatio) {
62
+ ratio = (canvas.width / videoItem.videoSize.width).toFloat()
63
+ ratioX = true
54
64
sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
55
65
sharedContentTransform.postTranslate(0.0f, ((canvas.height - videoItem.videoSize.height * (canvas.width / videoItem.videoSize.width)) / 2.0).toFloat())
56
66
}
57
67
else {
68
+ ratio = (canvas.height / videoItem.videoSize.height).toFloat()
69
+ ratioX = false
58
70
sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
59
71
sharedContentTransform.postTranslate(((canvas.width - videoItem.videoSize.width * (canvas.height / videoItem.videoSize.height)) / 2.0).toFloat(), 0.0f)
60
72
}
@@ -64,10 +76,14 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
64
76
val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
65
77
val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
66
78
if (videoRatio > canvasRatio) {
79
+ ratio = (canvas.width / videoItem.videoSize.width).toFloat()
80
+ ratioX = true
67
81
sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
68
82
sharedContentTransform.postTranslate(0.0f, ((canvas.height - videoItem.videoSize.height * (canvas.width / videoItem.videoSize.width)) / 2.0).toFloat())
69
83
}
70
84
else {
85
+ ratio = (canvas.height / videoItem.videoSize.height).toFloat()
86
+ ratioX = false
71
87
sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
72
88
sharedContentTransform.postTranslate(((canvas.width - videoItem.videoSize.width * (canvas.height / videoItem.videoSize.height)) / 2.0).toFloat(), 0.0f)
73
89
}
@@ -76,28 +92,40 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
76
92
val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
77
93
val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
78
94
if (videoRatio > canvasRatio) {
95
+ ratio = (canvas.width / videoItem.videoSize.width).toFloat()
96
+ ratioX = true
79
97
sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
80
98
}
81
99
else {
100
+ ratio = (canvas.height / videoItem.videoSize.height).toFloat()
101
+ ratioX = false
82
102
sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
83
103
}
84
104
}
85
105
ImageView.ScaleType.FIT_END -> {
86
106
val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
87
107
val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
88
108
if (videoRatio > canvasRatio) {
109
+ ratio = (canvas.width / videoItem.videoSize.width).toFloat()
110
+ ratioX = true
89
111
sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
90
112
sharedContentTransform.postTranslate(0.0f, (canvas.height - videoItem.videoSize.height * (canvas.width / videoItem.videoSize.width)).toFloat())
91
113
}
92
114
else {
115
+ ratio = (canvas.height / videoItem.videoSize.height).toFloat()
116
+ ratioX = false
93
117
sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
94
118
sharedContentTransform.postTranslate((canvas.width - videoItem.videoSize.width * (canvas.height / videoItem.videoSize.height)).toFloat(), 0.0f)
95
119
}
96
120
}
97
121
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()
98
124
sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
99
125
}
100
126
else -> {
127
+ ratio = (canvas.width / videoItem.videoSize.width).toFloat()
128
+ ratioX = true
101
129
sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
102
130
}
103
131
}
@@ -109,6 +137,7 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
109
137
}
110
138
111
139
private fun drawImage(sprite: SVGADrawerSprite, scaleType: ImageView.ScaleType) {
140
+ val canvas = this.canvas ?: return
112
141
(dynamicItem.dynamicImage[sprite.imageKey] ?: videoItem.images[sprite.imageKey])?.let {
113
142
sharedPaint.reset()
114
143
sharedContentTransform.reset()
@@ -135,6 +164,7 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
135
164
}
136
165
137
166
private fun drawText(drawingBitmap: Bitmap, sprite: SVGADrawerSprite) {
167
+ val canvas = this.canvas ?: return
138
168
dynamicItem.dynamicText[sprite.imageKey]?.let { drawingText ->
139
169
dynamicItem.dynamicTextPaint[sprite.imageKey]?.let { drawingTextPaint ->
140
170
val textBitmap = Bitmap.createBitmap(drawingBitmap.width, drawingBitmap.height, Bitmap.Config.ARGB_8888)
@@ -168,6 +198,7 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
168
198
}
169
199
170
200
private fun drawShape(sprite: SVGADrawerSprite, scaleType: ImageView.ScaleType) {
201
+ val canvas = this.canvas ?: return
171
202
sharedContentTransform.reset()
172
203
performScaleType(scaleType)
173
204
sharedContentTransform.preConcat(sprite.frameEntity.transform)
@@ -221,15 +252,44 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
221
252
}
222
253
}
223
254
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
+
224
283
private fun resetShapeStrokePaint(shape: SVGAVideoShapeEntity) {
284
+
225
285
sharedPaint.reset()
226
286
sharedPaint.isAntiAlias = true
227
287
sharedPaint.style = Paint.Style.STROKE
228
288
shape.styles?.stroke?.let {
229
289
sharedPaint.color = it
230
290
}
231
291
shape.styles?.strokeWidth?.let {
232
- sharedPaint.strokeWidth = it
292
+ sharedPaint.strokeWidth = it * requestScale()
233
293
}
234
294
shape.styles?.lineCap?.let {
235
295
when {
@@ -246,14 +306,14 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
246
306
}
247
307
}
248
308
shape.styles?.miterLimit?.let {
249
- sharedPaint.strokeMiter = it.toFloat()
309
+ sharedPaint.strokeMiter = it.toFloat() * requestScale()
250
310
}
251
311
shape.styles?.lineDash?.let {
252
312
if (it.size == 3) {
253
313
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() )
257
317
}
258
318
}
259
319
}
0 commit comments