@@ -11,8 +11,8 @@ import android.widget.ImageView
11
11
class SVGACanvasDrawer (videoItem : SVGAVideoEntity , val dynamicItem : SVGADynamicEntity ) : SGVADrawer(videoItem) {
12
12
13
13
var canvas: Canvas ? = null
14
- private var ratio = 1.0f
15
- private var ratioX = false
14
+ var scaleEntity : ScaleEntity = ScaleEntity ()
15
+
16
16
private val sharedPaint = Paint ()
17
17
private val sharedPath = Path ()
18
18
private val sharedPath2 = Path ()
@@ -21,130 +21,36 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
21
21
override fun drawFrame (frameIndex : Int , scaleType : ImageView .ScaleType ) {
22
22
super .drawFrame(frameIndex, scaleType)
23
23
val sprites = requestFrameSprites(frameIndex)
24
+ performScaleType(scaleType)
24
25
sprites.forEach {
25
- drawSprite(it, scaleType )
26
+ drawSprite(it)
26
27
}
27
28
}
28
29
30
+ private fun enableScaleEntity (){
31
+ sharedContentTransform.reset()
32
+ sharedContentTransform.postScale(scaleEntity.scaleFx, scaleEntity.scaleFy)
33
+ sharedContentTransform.postTranslate(scaleEntity.tranFx, scaleEntity.tranFy)
34
+ }
35
+
29
36
private fun performScaleType (scaleType : ImageView .ScaleType ) {
30
37
val canvas = this .canvas ? : return
31
- if (canvas.width == 0 || canvas.height == 0 || videoItem.videoSize.width == 0.0 || videoItem.videoSize.height == 0.0 ) {
32
- return
33
- }
34
- when (scaleType) {
35
- ImageView .ScaleType .CENTER -> {
36
- sharedContentTransform.postTranslate(((canvas.width - videoItem.videoSize.width) / 2.0 ).toFloat(), ((canvas.height - videoItem.videoSize.height) / 2.0 ).toFloat())
37
- }
38
- ImageView .ScaleType .CENTER_CROP -> {
39
- val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
40
- val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
41
- if (videoRatio > canvasRatio) {
42
- ratio = (canvas.height / videoItem.videoSize.height).toFloat()
43
- ratioX = false
44
- sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
45
- sharedContentTransform.postTranslate(((canvas.width - videoItem.videoSize.width * (canvas.height / videoItem.videoSize.height)) / 2.0 ).toFloat(), 0.0f )
46
- }
47
- else {
48
- ratio = (canvas.width / videoItem.videoSize.width).toFloat()
49
- ratioX = true
50
- sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
51
- sharedContentTransform.postTranslate(0.0f , ((canvas.height - videoItem.videoSize.height * (canvas.width / videoItem.videoSize.width)) / 2.0 ).toFloat())
52
- }
53
- }
54
- ImageView .ScaleType .CENTER_INSIDE -> {
55
- if (videoItem.videoSize.width < canvas.width && videoItem.videoSize.height < canvas.height) {
56
- sharedContentTransform.postTranslate(((canvas.width - videoItem.videoSize.width) / 2.0 ).toFloat(), ((canvas.height - videoItem.videoSize.height) / 2.0 ).toFloat())
57
- }
58
- else {
59
- val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
60
- val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
61
- if (videoRatio > canvasRatio) {
62
- ratio = (canvas.width / videoItem.videoSize.width).toFloat()
63
- ratioX = true
64
- sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
65
- sharedContentTransform.postTranslate(0.0f , ((canvas.height - videoItem.videoSize.height * (canvas.width / videoItem.videoSize.width)) / 2.0 ).toFloat())
66
- }
67
- else {
68
- ratio = (canvas.height / videoItem.videoSize.height).toFloat()
69
- ratioX = false
70
- sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
71
- sharedContentTransform.postTranslate(((canvas.width - videoItem.videoSize.width * (canvas.height / videoItem.videoSize.height)) / 2.0 ).toFloat(), 0.0f )
72
- }
73
- }
74
- }
75
- ImageView .ScaleType .FIT_CENTER -> {
76
- val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
77
- val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
78
- if (videoRatio > canvasRatio) {
79
- ratio = (canvas.width / videoItem.videoSize.width).toFloat()
80
- ratioX = true
81
- sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
82
- sharedContentTransform.postTranslate(0.0f , ((canvas.height - videoItem.videoSize.height * (canvas.width / videoItem.videoSize.width)) / 2.0 ).toFloat())
83
- }
84
- else {
85
- ratio = (canvas.height / videoItem.videoSize.height).toFloat()
86
- ratioX = false
87
- sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
88
- sharedContentTransform.postTranslate(((canvas.width - videoItem.videoSize.width * (canvas.height / videoItem.videoSize.height)) / 2.0 ).toFloat(), 0.0f )
89
- }
90
- }
91
- ImageView .ScaleType .FIT_START -> {
92
- val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
93
- val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
94
- if (videoRatio > canvasRatio) {
95
- ratio = (canvas.width / videoItem.videoSize.width).toFloat()
96
- ratioX = true
97
- sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
98
- }
99
- else {
100
- ratio = (canvas.height / videoItem.videoSize.height).toFloat()
101
- ratioX = false
102
- sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
103
- }
104
- }
105
- ImageView .ScaleType .FIT_END -> {
106
- val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
107
- val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
108
- if (videoRatio > canvasRatio) {
109
- ratio = (canvas.width / videoItem.videoSize.width).toFloat()
110
- ratioX = true
111
- sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
112
- sharedContentTransform.postTranslate(0.0f , (canvas.height - videoItem.videoSize.height * (canvas.width / videoItem.videoSize.width)).toFloat())
113
- }
114
- else {
115
- ratio = (canvas.height / videoItem.videoSize.height).toFloat()
116
- ratioX = false
117
- sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
118
- sharedContentTransform.postTranslate((canvas.width - videoItem.videoSize.width * (canvas.height / videoItem.videoSize.height)).toFloat(), 0.0f )
119
- }
120
- }
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()
124
- sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
125
- }
126
- else -> {
127
- ratio = (canvas.width / videoItem.videoSize.width).toFloat()
128
- ratioX = true
129
- sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
130
- }
131
- }
38
+ scaleEntity.performScaleType(canvas.width.toFloat(),canvas.height.toFloat(),videoItem.videoSize.width.toFloat(),videoItem.videoSize.height.toFloat(),scaleType)
132
39
}
133
40
134
- private fun drawSprite (sprite : SVGADrawerSprite , scaleType : ImageView . ScaleType ) {
135
- drawImage(sprite, scaleType )
136
- drawShape(sprite, scaleType )
41
+ private fun drawSprite (sprite : SVGADrawerSprite ) {
42
+ drawImage(sprite)
43
+ drawShape(sprite)
137
44
}
138
45
139
- private fun drawImage (sprite : SVGADrawerSprite , scaleType : ImageView . ScaleType ) {
46
+ private fun drawImage (sprite : SVGADrawerSprite ) {
140
47
val canvas = this .canvas ? : return
141
48
(dynamicItem.dynamicImage[sprite.imageKey] ? : videoItem.images[sprite.imageKey])?.let {
142
49
sharedPaint.reset()
143
- sharedContentTransform.reset()
144
50
sharedPaint.isAntiAlias = videoItem.antiAlias
145
51
sharedPaint.isFilterBitmap = videoItem.antiAlias
146
52
sharedPaint.alpha = (sprite.frameEntity.alpha * 255 ).toInt()
147
- performScaleType(scaleType )
53
+ enableScaleEntity( )
148
54
sharedContentTransform.preConcat(sprite.frameEntity.transform)
149
55
if (sprite.frameEntity.maskPath != null ) {
150
56
val maskPath = sprite.frameEntity.maskPath ? : return @let
@@ -202,10 +108,9 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
202
108
}
203
109
}
204
110
205
- private fun drawShape (sprite : SVGADrawerSprite , scaleType : ImageView . ScaleType ) {
111
+ private fun drawShape (sprite : SVGADrawerSprite ) {
206
112
val canvas = this .canvas ? : return
207
- sharedContentTransform.reset()
208
- performScaleType(scaleType)
113
+ enableScaleEntity()
209
114
sharedContentTransform.preConcat(sprite.frameEntity.transform)
210
115
sprite.frameEntity.shapes.forEach { shape ->
211
116
sharedPath.reset()
@@ -280,7 +185,7 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
280
185
if ( A * D < B * C ) {
281
186
scaleX = - scaleX
282
187
}
283
- return if (this .ratioX) ratio / Math .abs(scaleX.toFloat()) else ratio / Math .abs(scaleY.toFloat())
188
+ return if (scaleEntity .ratioX) scaleEntity. ratio / Math .abs(scaleX.toFloat()) else scaleEntity. ratio / Math .abs(scaleY.toFloat())
284
189
}
285
190
286
191
private fun resetShapeStrokePaint (shape : SVGAVideoShapeEntity ) {
0 commit comments