@@ -3,7 +3,7 @@ package com.opensource.svgaplayer
3
3
import android.graphics.*
4
4
import android.os.Build
5
5
import android.R.attr.x
6
-
6
+ import android.widget.ImageView
7
7
8
8
9
9
/* *
@@ -16,27 +16,107 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
16
16
val sharedPath = Path ()
17
17
val sharedContentTransform = Matrix ()
18
18
19
- override fun drawFrame (frameIndex : Int ) {
20
- super .drawFrame(frameIndex)
19
+ override fun drawFrame (frameIndex : Int , scaleType : ImageView . ScaleType ) {
20
+ super .drawFrame(frameIndex, scaleType )
21
21
val sprites = requestFrameSprites(frameIndex)
22
22
sprites.forEach {
23
- drawSprite(it)
23
+ drawSprite(it, scaleType)
24
+ }
25
+ }
26
+
27
+ private fun performScaleType (scaleType : ImageView .ScaleType ) {
28
+ if (canvas.width == 0 || canvas.height == 0 || videoItem.videoSize.width == 0.0 || videoItem.videoSize.height == 0.0 ) {
29
+ return
30
+ }
31
+ when (scaleType) {
32
+ ImageView .ScaleType .CENTER -> {
33
+ sharedContentTransform.postTranslate(((canvas.width - videoItem.videoSize.width) / 2.0 ).toFloat(), ((canvas.height - videoItem.videoSize.height) / 2.0 ).toFloat())
34
+ }
35
+ ImageView .ScaleType .CENTER_CROP -> {
36
+ val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
37
+ val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
38
+ if (videoRatio > canvasRatio) {
39
+ sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
40
+ sharedContentTransform.postTranslate(((canvas.width - videoItem.videoSize.width * (canvas.height / videoItem.videoSize.height)) / 2.0 ).toFloat(), 0.0f )
41
+ }
42
+ else {
43
+ sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
44
+ sharedContentTransform.postTranslate(0.0f , ((canvas.height - videoItem.videoSize.height * (canvas.width / videoItem.videoSize.width)) / 2.0 ).toFloat())
45
+ }
46
+ }
47
+ ImageView .ScaleType .CENTER_INSIDE -> {
48
+ if (videoItem.videoSize.width < canvas.width && videoItem.videoSize.height < canvas.height) {
49
+ sharedContentTransform.postTranslate(((canvas.width - videoItem.videoSize.width) / 2.0 ).toFloat(), ((canvas.height - videoItem.videoSize.height) / 2.0 ).toFloat())
50
+ }
51
+ else {
52
+ val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
53
+ val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
54
+ if (videoRatio > canvasRatio) {
55
+ sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
56
+ sharedContentTransform.postTranslate(0.0f , ((canvas.height - videoItem.videoSize.height * (canvas.width / videoItem.videoSize.width)) / 2.0 ).toFloat())
57
+ }
58
+ else {
59
+ sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
60
+ sharedContentTransform.postTranslate(((canvas.width - videoItem.videoSize.width * (canvas.height / videoItem.videoSize.height)) / 2.0 ).toFloat(), 0.0f )
61
+ }
62
+ }
63
+ }
64
+ ImageView .ScaleType .FIT_CENTER -> {
65
+ val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
66
+ val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
67
+ if (videoRatio > canvasRatio) {
68
+ sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
69
+ sharedContentTransform.postTranslate(0.0f , ((canvas.height - videoItem.videoSize.height * (canvas.width / videoItem.videoSize.width)) / 2.0 ).toFloat())
70
+ }
71
+ else {
72
+ sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
73
+ sharedContentTransform.postTranslate(((canvas.width - videoItem.videoSize.width * (canvas.height / videoItem.videoSize.height)) / 2.0 ).toFloat(), 0.0f )
74
+ }
75
+ }
76
+ ImageView .ScaleType .FIT_START -> {
77
+ val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
78
+ val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
79
+ if (videoRatio > canvasRatio) {
80
+ sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
81
+ }
82
+ else {
83
+ sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
84
+ }
85
+ }
86
+ ImageView .ScaleType .FIT_END -> {
87
+ val videoRatio = (videoItem.videoSize.width / videoItem.videoSize.height)
88
+ val canvasRatio = canvas.width.toFloat() / canvas.height.toFloat()
89
+ if (videoRatio > canvasRatio) {
90
+ sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
91
+ sharedContentTransform.postTranslate(0.0f , (canvas.height - videoItem.videoSize.height * (canvas.width / videoItem.videoSize.width)).toFloat())
92
+ }
93
+ else {
94
+ sharedContentTransform.postScale((canvas.height / videoItem.videoSize.height).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
95
+ sharedContentTransform.postTranslate((canvas.width - videoItem.videoSize.width * (canvas.height / videoItem.videoSize.height)).toFloat(), 0.0f )
96
+ }
97
+ }
98
+ ImageView .ScaleType .FIT_XY -> {
99
+ sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.height / videoItem.videoSize.height).toFloat())
100
+ }
101
+ else -> {
102
+ sharedContentTransform.postScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
103
+ }
24
104
}
25
105
}
26
106
27
- private fun drawSprite (sprite : SVGADrawerSprite ) {
28
- drawImage(sprite)
29
- drawShape(sprite)
107
+ private fun drawSprite (sprite : SVGADrawerSprite , scaleType : ImageView . ScaleType ) {
108
+ drawImage(sprite, scaleType )
109
+ drawShape(sprite, scaleType )
30
110
}
31
111
32
- private fun drawImage (sprite : SVGADrawerSprite ) {
112
+ private fun drawImage (sprite : SVGADrawerSprite , scaleType : ImageView . ScaleType ) {
33
113
(dynamicItem.dynamicImage[sprite.imageKey] ? : videoItem.images[sprite.imageKey])?.let {
34
114
val drawingBitmap = it
35
115
sharedPaint.reset()
36
116
sharedContentTransform.reset()
37
117
sharedPaint.isAntiAlias = videoItem.antiAlias
38
118
sharedPaint.alpha = (sprite.frameEntity.alpha * 255 ).toInt()
39
- sharedContentTransform.setScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat() )
119
+ performScaleType(scaleType )
40
120
sharedContentTransform.preConcat(sprite.frameEntity.transform)
41
121
sharedContentTransform.preScale((sprite.frameEntity.layout.width / drawingBitmap.width).toFloat(), (sprite.frameEntity.layout.width / drawingBitmap.width).toFloat())
42
122
if (sprite.frameEntity.maskPath != null ) {
@@ -88,9 +168,9 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
88
168
}
89
169
}
90
170
91
- private fun drawShape (sprite : SVGADrawerSprite ) {
171
+ private fun drawShape (sprite : SVGADrawerSprite , scaleType : ImageView . ScaleType ) {
92
172
sharedContentTransform.reset()
93
- sharedContentTransform.setScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat() )
173
+ performScaleType(scaleType )
94
174
sharedContentTransform.preConcat(sprite.frameEntity.transform)
95
175
sprite.frameEntity.shapes.forEach {
96
176
val shape = it
0 commit comments