Skip to content

Commit 3d283ed

Browse files
authored
Update SVGACanvasDrawer.kt
use scaleEntiity for performScaleType, every frame only perform once(maybe once globle better)
1 parent 5a0651f commit 3d283ed

File tree

1 file changed

+19
-114
lines changed

1 file changed

+19
-114
lines changed

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

Lines changed: 19 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import android.widget.ImageView
1111
class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicEntity) : SGVADrawer(videoItem) {
1212

1313
var canvas: Canvas? = null
14-
private var ratio = 1.0f
15-
private var ratioX = false
14+
var scaleEntity:ScaleEntity = ScaleEntity()
15+
1616
private val sharedPaint = Paint()
1717
private val sharedPath = Path()
1818
private val sharedPath2 = Path()
@@ -21,130 +21,36 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
2121
override fun drawFrame(frameIndex: Int, scaleType: ImageView.ScaleType) {
2222
super.drawFrame(frameIndex, scaleType)
2323
val sprites = requestFrameSprites(frameIndex)
24+
performScaleType(scaleType)
2425
sprites.forEach {
25-
drawSprite(it, scaleType)
26+
drawSprite(it)
2627
}
2728
}
2829

30+
private fun enableScaleEntity(){
31+
sharedContentTransform.reset()
32+
sharedContentTransform.postScale(scaleEntity.scaleFx, scaleEntity.scaleFy)
33+
sharedContentTransform.postTranslate(scaleEntity.tranFx, scaleEntity.tranFy)
34+
}
35+
2936
private fun performScaleType(scaleType: ImageView.ScaleType) {
3037
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)
13239
}
13340

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)
13744
}
13845

139-
private fun drawImage(sprite: SVGADrawerSprite, scaleType: ImageView.ScaleType) {
46+
private fun drawImage(sprite: SVGADrawerSprite) {
14047
val canvas = this.canvas ?: return
14148
(dynamicItem.dynamicImage[sprite.imageKey] ?: videoItem.images[sprite.imageKey])?.let {
14249
sharedPaint.reset()
143-
sharedContentTransform.reset()
14450
sharedPaint.isAntiAlias = videoItem.antiAlias
14551
sharedPaint.isFilterBitmap = videoItem.antiAlias
14652
sharedPaint.alpha = (sprite.frameEntity.alpha * 255).toInt()
147-
performScaleType(scaleType)
53+
enableScaleEntity()
14854
sharedContentTransform.preConcat(sprite.frameEntity.transform)
14955
if (sprite.frameEntity.maskPath != null) {
15056
val maskPath = sprite.frameEntity.maskPath ?: return@let
@@ -202,10 +108,9 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
202108
}
203109
}
204110

205-
private fun drawShape(sprite: SVGADrawerSprite, scaleType: ImageView.ScaleType) {
111+
private fun drawShape(sprite: SVGADrawerSprite) {
206112
val canvas = this.canvas ?: return
207-
sharedContentTransform.reset()
208-
performScaleType(scaleType)
113+
enableScaleEntity()
209114
sharedContentTransform.preConcat(sprite.frameEntity.transform)
210115
sprite.frameEntity.shapes.forEach { shape ->
211116
sharedPath.reset()
@@ -280,7 +185,7 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
280185
if ( A * D < B * C ) {
281186
scaleX = -scaleX
282187
}
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())
284189
}
285190

286191
private fun resetShapeStrokePaint(shape: SVGAVideoShapeEntity) {

0 commit comments

Comments
 (0)