Skip to content

Commit b6050c8

Browse files
authored
Merge pull request svga#33 from andyliumstar/master
Separate performScaleType
2 parents b6ef6a1 + 3d283ed commit b6050c8

File tree

2 files changed

+164
-114
lines changed

2 files changed

+164
-114
lines changed

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

+19-114
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) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package com.opensource.svgaplayer
2+
3+
import android.widget.ImageView
4+
5+
/**
6+
* Created by ubt on 2018/1/19.
7+
*/
8+
class ScaleEntity {
9+
var tranFx : Float = 0.0f
10+
var tranFy : Float = 0.0f
11+
var scaleFx : Float = 1.0f
12+
var scaleFy : Float = 1.0f
13+
var ratio = 1.0f
14+
var ratioX = false
15+
16+
private fun resetVar(){
17+
tranFx = 0.0f
18+
tranFy = 0.0f
19+
scaleFx = 1.0f
20+
scaleFy = 1.0f
21+
ratio = 1.0f
22+
ratioX = false
23+
}
24+
25+
fun performScaleType(canvasWidth : Float, canvasHeight: Float, videoWidth : Float, videoHeight : Float, scaleType: ImageView.ScaleType) {
26+
if (canvasWidth == 0.0f || canvasHeight == 0.0f || videoWidth == 0.0f || videoHeight == 0.0f) {
27+
return
28+
}
29+
30+
resetVar()
31+
val canW_vidW_f = (canvasWidth - videoWidth) / 2.0f
32+
val canH_vidH_f = (canvasHeight - videoHeight) / 2.0f
33+
34+
val videoRatio = videoWidth / videoHeight
35+
val canvasRatio = canvasWidth / canvasHeight
36+
37+
val canH_d_vidH = canvasHeight / videoHeight
38+
val canW_d_vidW = canvasWidth / videoWidth
39+
40+
when (scaleType) {
41+
ImageView.ScaleType.CENTER -> {
42+
tranFx = canW_vidW_f
43+
tranFy = canH_vidH_f
44+
}
45+
ImageView.ScaleType.CENTER_CROP -> {
46+
if (videoRatio > canvasRatio) {
47+
ratio = canH_d_vidH
48+
ratioX = false
49+
scaleFx = canH_d_vidH
50+
scaleFy = canH_d_vidH
51+
tranFx = (canvasWidth - videoWidth * (canH_d_vidH)) / 2.0f
52+
}
53+
else {
54+
ratio = canW_d_vidW
55+
ratioX = true
56+
scaleFx = canW_d_vidW
57+
scaleFy = canW_d_vidW
58+
tranFy = (canvasHeight - videoHeight * (canW_d_vidW)) / 2.0f
59+
}
60+
}
61+
ImageView.ScaleType.CENTER_INSIDE -> {
62+
if (videoWidth < canvasWidth && videoHeight < canvasHeight) {
63+
tranFx = canW_vidW_f
64+
tranFy = canH_vidH_f
65+
}
66+
else {
67+
if (videoRatio > canvasRatio) {
68+
ratio = canW_d_vidW
69+
ratioX = true
70+
scaleFx = canW_d_vidW
71+
scaleFy = canW_d_vidW
72+
tranFy = (canvasHeight - videoHeight * (canW_d_vidW)) / 2.0f
73+
74+
}
75+
else {
76+
ratio = canH_d_vidH
77+
ratioX = false
78+
scaleFx = canH_d_vidH
79+
scaleFy = canH_d_vidH
80+
tranFx = (canvasWidth - videoWidth * (canH_d_vidH)) / 2.0f
81+
}
82+
}
83+
}
84+
ImageView.ScaleType.FIT_CENTER -> {
85+
if (videoRatio > canvasRatio) {
86+
ratio = canW_d_vidW
87+
ratioX = true
88+
scaleFx = canW_d_vidW
89+
scaleFy = canW_d_vidW
90+
tranFy = (canvasHeight - videoHeight * (canW_d_vidW)) / 2.0f
91+
}
92+
else {
93+
ratio = canH_d_vidH
94+
ratioX = false
95+
scaleFx = canH_d_vidH
96+
scaleFy = canH_d_vidH
97+
tranFx = (canvasWidth - videoWidth * (canH_d_vidH)) / 2.0f
98+
}
99+
}
100+
ImageView.ScaleType.FIT_START -> {
101+
if (videoRatio > canvasRatio) {
102+
ratio = canW_d_vidW
103+
ratioX = true
104+
scaleFx = canW_d_vidW
105+
scaleFy = canW_d_vidW
106+
}
107+
else {
108+
ratio = canH_d_vidH
109+
ratioX = false
110+
scaleFx = canH_d_vidH
111+
scaleFy = canH_d_vidH
112+
}
113+
}
114+
ImageView.ScaleType.FIT_END -> {
115+
if (videoRatio > canvasRatio) {
116+
ratio = canW_d_vidW
117+
ratioX = true
118+
scaleFx = canW_d_vidW
119+
scaleFy = canW_d_vidW
120+
tranFy= canvasHeight - videoHeight * (canW_d_vidW)
121+
}
122+
else {
123+
ratio = canH_d_vidH
124+
ratioX = false
125+
scaleFx = canH_d_vidH
126+
scaleFy = canH_d_vidH
127+
tranFx = canvasWidth - videoWidth * (canH_d_vidH)
128+
}
129+
}
130+
ImageView.ScaleType.FIT_XY -> {
131+
ratio = Math.max(canW_d_vidW, canH_d_vidH)
132+
ratioX = canW_d_vidW > canH_d_vidH
133+
scaleFx = canW_d_vidW
134+
scaleFy = canH_d_vidH
135+
}
136+
else -> {
137+
ratio = canW_d_vidW
138+
ratioX = true
139+
scaleFx = canW_d_vidW
140+
scaleFy = canW_d_vidW
141+
}
142+
}
143+
}
144+
145+
}

0 commit comments

Comments
 (0)