Skip to content

Commit 025a95a

Browse files
调整 SVGAVideoEntity 资源初始化时机,可传递 View size 计算 Bitmp 缩放
1 parent 4c50963 commit 025a95a

3 files changed

Lines changed: 47 additions & 33 deletions

File tree

‎app/src/main/java/com/example/ponycui_home/svgaplayer/AnimationFromAssetsActivity.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.jetbrains.annotations.NotNull;
1515

1616
import java.util.ArrayList;
17-
import java.util.logging.Logger;
1817

1918
public class AnimationFromAssetsActivity extends Activity {
2019

@@ -70,7 +69,6 @@ private String randomSample() {
7069
samples.add("MerryChristmas.svga");
7170
}
7271
return samples.get((int) Math.floor(Math.random() * samples.size()));
73-
// return "gradientBorder.svga";
7472
}
7573

7674
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ open class SVGAImageView @JvmOverloads constructor(context: Context, attrs: Attr
141141
val drawable = getSVGADrawable() ?: return
142142
drawable.cleared = false
143143
drawable.scaleType = scaleType
144-
drawable.videoItem.reqHeight = height
145-
drawable.videoItem.reqWidth = width
144+
drawable.videoItem.init(width, height)
146145
Log.d("SVGAImageView", "height:$height width:$width")
147146
}
148147

‎library/src/main/java/com/opensource/svgaplayer/SVGAVideoEntity.kt‎

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,43 +35,22 @@ class SVGAVideoEntity {
3535
var frames: Int = 0
3636
private set
3737

38-
internal var reqHeight = 0
39-
internal var reqWidth = 0
38+
private var reqHeight = 0
39+
private var reqWidth = 0
4040
internal var spriteList: List<SVGAVideoSpriteEntity> = emptyList()
4141
internal var audioList: List<SVGAAudioEntity> = emptyList()
4242
internal var soundPool: SoundPool? = null
4343
internal var imageMap = HashMap<String, Bitmap>()
4444
private var mCacheDir: File
45+
private var mJsonMovie: JSONObject? = null
4546

4647
constructor(json: JSONObject, cacheDir: File) {
47-
this.mCacheDir = cacheDir
48-
json.optJSONObject("movie")?.let(this::setup)
49-
try {
50-
parserImages(json)
51-
} catch (e: Exception) {
52-
e.printStackTrace()
53-
} catch (e: OutOfMemoryError) {
54-
e.printStackTrace()
55-
}
56-
resetSprites(json)
57-
}
58-
59-
internal constructor(entity: MovieEntity, cacheDir: File) {
60-
this.movieItem = entity
61-
this.mCacheDir = cacheDir
62-
entity.params?.let (this::setup)
63-
64-
try {
65-
parserImages(entity)
66-
} catch (e: Exception) {
67-
e.printStackTrace()
68-
} catch (e: OutOfMemoryError) {
69-
e.printStackTrace()
70-
}
71-
resetSprites(entity)
48+
mJsonMovie = json
49+
mCacheDir = cacheDir
50+
json.optJSONObject("movie")?.let(this::setupByJson)
7251
}
7352

74-
private fun setup(movieObject: JSONObject) {
53+
private fun setupByJson(movieObject: JSONObject) {
7554
movieObject.optJSONObject("viewBox")?.let { viewBoxObject ->
7655
val width = viewBoxObject.optDouble("width", 0.0)
7756
val height = viewBoxObject.optDouble("height", 0.0)
@@ -81,14 +60,52 @@ class SVGAVideoEntity {
8160
frames = movieObject.optInt("frames", 0)
8261
}
8362

84-
private fun setup(movieParams: MovieParams) {
63+
internal constructor(entity: MovieEntity, cacheDir: File) {
64+
this.movieItem = entity
65+
this.mCacheDir = cacheDir
66+
entity.params?.let (this::setupByMovie)
67+
}
68+
69+
private fun setupByMovie(movieParams: MovieParams) {
8570
val width = (movieParams.viewBoxWidth ?: 0.0f).toDouble()
8671
val height = (movieParams.viewBoxHeight ?: 0.0f).toDouble()
8772
videoSize = SVGARect(0.0, 0.0, width, height)
8873
FPS = movieParams.fps ?: 20
8974
frames = movieParams.frames ?: 0
9075
}
9176

77+
internal fun init(reqWidth:Int, reqHeight:Int) {
78+
this.reqWidth = reqWidth
79+
this.reqHeight = reqHeight
80+
if (mJsonMovie != null) {
81+
parsResourceByJson()
82+
} else if (movieItem != null) {
83+
parsResourceByMovie()
84+
}
85+
}
86+
87+
private fun parsResourceByJson() {
88+
try {
89+
parserImages(mJsonMovie!!)
90+
} catch (e: Exception) {
91+
e.printStackTrace()
92+
} catch (e: OutOfMemoryError) {
93+
e.printStackTrace()
94+
}
95+
resetSprites(mJsonMovie!!)
96+
}
97+
98+
private fun parsResourceByMovie() {
99+
try {
100+
parserImages(movieItem!!)
101+
} catch (e: Exception) {
102+
e.printStackTrace()
103+
} catch (e: OutOfMemoryError) {
104+
e.printStackTrace()
105+
}
106+
resetSprites(movieItem!!)
107+
}
108+
92109
internal fun prepare(callback: () -> Unit) {
93110
if (movieItem == null) {
94111
callback()

0 commit comments

Comments
 (0)