Skip to content

Commit c07409c

Browse files
committed
feature: add scaleType support.
1 parent 5735cff commit c07409c

File tree

5 files changed

+102
-15
lines changed

5 files changed

+102
-15
lines changed

app/src/main/assets/750x80.svga

10.9 KB
Binary file not shown.

app/src/main/res/layout/activity_simple.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
android:orientation="vertical"
55
android:layout_width="match_parent"
6-
android:layout_height="match_parent">
6+
android:layout_height="match_parent"
7+
android:background="#000000">
78

89
<com.opensource.svgaplayer.SVGAImageView
910
android:layout_height="match_parent"
1011
android:layout_width="match_parent"
11-
app:source="posche.svga"
12+
android:scaleType="fitCenter"
13+
app:source="750x80.svga"
1214
app:antiAlias="true"/>
1315

1416
</RelativeLayout>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.opensource.svgaplayer
22

3+
import android.widget.ImageView
4+
35
/**
46
* Created by cuiminghui on 2017/3/29.
57
*/
@@ -20,7 +22,7 @@ open class SGVADrawer(val videoItem: SVGAVideoEntity) {
2022
}
2123
}
2224

23-
open fun drawFrame(frameIndex: Int) {
25+
open fun drawFrame(frameIndex: Int, scaleType: ImageView.ScaleType) {
2426

2527
}
2628

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

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.opensource.svgaplayer
33
import android.graphics.*
44
import android.os.Build
55
import android.R.attr.x
6-
6+
import android.widget.ImageView
77

88

99
/**
@@ -16,27 +16,107 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
1616
val sharedPath = Path()
1717
val sharedContentTransform = Matrix()
1818

19-
override fun drawFrame(frameIndex: Int) {
20-
super.drawFrame(frameIndex)
19+
override fun drawFrame(frameIndex: Int, scaleType: ImageView.ScaleType) {
20+
super.drawFrame(frameIndex, scaleType)
2121
val sprites = requestFrameSprites(frameIndex)
2222
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+
}
24104
}
25105
}
26106

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)
30110
}
31111

32-
private fun drawImage(sprite: SVGADrawerSprite) {
112+
private fun drawImage(sprite: SVGADrawerSprite, scaleType: ImageView.ScaleType) {
33113
(dynamicItem.dynamicImage[sprite.imageKey] ?: videoItem.images[sprite.imageKey])?.let {
34114
val drawingBitmap = it
35115
sharedPaint.reset()
36116
sharedContentTransform.reset()
37117
sharedPaint.isAntiAlias = videoItem.antiAlias
38118
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)
40120
sharedContentTransform.preConcat(sprite.frameEntity.transform)
41121
sharedContentTransform.preScale((sprite.frameEntity.layout.width / drawingBitmap.width).toFloat(), (sprite.frameEntity.layout.width / drawingBitmap.width).toFloat())
42122
if (sprite.frameEntity.maskPath != null) {
@@ -88,9 +168,9 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
88168
}
89169
}
90170

91-
private fun drawShape(sprite: SVGADrawerSprite) {
171+
private fun drawShape(sprite: SVGADrawerSprite, scaleType: ImageView.ScaleType) {
92172
sharedContentTransform.reset()
93-
sharedContentTransform.setScale((canvas.width / videoItem.videoSize.width).toFloat(), (canvas.width / videoItem.videoSize.width).toFloat())
173+
performScaleType(scaleType)
94174
sharedContentTransform.preConcat(sprite.frameEntity.transform)
95175
sprite.frameEntity.shapes.forEach {
96176
val shape = it

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ class SVGADrawable(val videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
4141
invalidateSelf()
4242
}
4343

44+
var scaleType: ImageView.ScaleType = ImageView.ScaleType.MATRIX
45+
4446
override fun draw(canvas: Canvas?) {
4547
if (cleared) {
4648
return
4749
}
4850
canvas?.let {
4951
val drawer = SVGACanvasDrawer(videoItem, dynamicItem, it)
50-
drawer.drawFrame(currentFrame)
52+
drawer.drawFrame(currentFrame, scaleType)
5153
}
5254
}
5355

@@ -152,6 +154,7 @@ open class SVGAImageView : ImageView {
152154
fun startAnimation() {
153155
val drawable = drawable as? SVGADrawable ?: return
154156
drawable.cleared = false
157+
drawable.scaleType = scaleType
155158
drawable.videoItem?.let {
156159
var durationScale = 1.0
157160
val animator = ValueAnimator.ofInt(0, it.frames - 1)

0 commit comments

Comments
 (0)