Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit 3a4d5b9

Browse files
committed
feat: Add shareParser.
1 parent 3bc8915 commit 3a4d5b9

File tree

6 files changed

+39
-23
lines changed

6 files changed

+39
-23
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public class AnimationFromAssetsActivity extends Activity {
1818

1919
int currentIndex = 0;
2020
SVGAImageView animationView = null;
21-
SVGAParser parser = new SVGAParser(this);
2221

2322
@Override
2423
protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -36,13 +35,13 @@ public void onClick(View view) {
3635
}
3736

3837
private void loadAnimation() {
39-
parser.decodeFromAssets(this.randomSample(), new SVGAParser.ParseCompletion() {
40-
@Override
41-
public void onComplete(@NotNull SVGAVideoEntity videoItem) {
42-
animationView.setVideoItem(videoItem);
43-
animationView.stepToFrame(0, true);
44-
}
45-
@Override
38+
SVGAParser.Companion.shareParser().decodeFromAssets(this.randomSample(), new SVGAParser.ParseCompletion() {
39+
@Override
40+
public void onComplete(@NotNull SVGAVideoEntity videoItem) {
41+
animationView.setVideoItem(videoItem);
42+
animationView.stepToFrame(0, true);
43+
}
44+
@Override
4645
public void onError() {
4746

4847
}

app/src/main/java/com/example/ponycui_home/svgaplayer/AnimationFromClickActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
public class AnimationFromClickActivity extends Activity {
2424

2525
SVGAImageView animationView = null;
26-
SVGAParser parser = new SVGAParser(this);
2726

2827
@Override
2928
protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -41,7 +40,7 @@ public void onClick(@NotNull String clickKey) {
4140
}
4241

4342
private void loadAnimation() {
44-
parser.decodeFromAssets("test2.svga",new SVGAParser.ParseCompletion() {
43+
SVGAParser.Companion.shareParser().decodeFromAssets("test2.svga",new SVGAParser.ParseCompletion() {
4544
@Override
4645
public void onComplete(@NotNull SVGAVideoEntity videoItem) {
4746
SVGADynamicEntity dynamicEntity = new SVGADynamicEntity();

app/src/main/java/com/example/ponycui_home/svgaplayer/AnimationFromNetworkActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
public class AnimationFromNetworkActivity extends Activity {
1818

1919
SVGAImageView animationView = null;
20-
SVGAParser parser = new SVGAParser(this);
2120

2221
@Override
2322
protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -30,7 +29,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
3029

3130
private void loadAnimation() {
3231
try { // new URL needs try catch.
33-
parser.decodeFromURL(new URL("https://github.com/yyued/SVGA-Samples/blob/master/posche.svga?raw=true"), new SVGAParser.ParseCompletion() {
32+
SVGAParser.Companion.shareParser().decodeFromURL(new URL("https://github.com/yyued/SVGA-Samples/blob/master/posche.svga?raw=true"), new SVGAParser.ParseCompletion() {
3433
@Override
3534
public void onComplete(@NotNull SVGAVideoEntity videoItem) {
3635
animationView.setVideoItem(videoItem);

app/src/main/java/com/example/ponycui_home/svgaplayer/MainActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
6767
super.onCreate(savedInstanceState);
6868
this.setupData();
6969
this.setupListView();
70+
this.setupSVGAParser();
7071
setContentView(listView);
7172
}
7273

@@ -155,6 +156,9 @@ public boolean isEmpty() {
155156
});
156157
this.listView.setBackgroundColor(Color.WHITE);
157158
}
159+
void setupSVGAParser() {
160+
SVGAParser.Companion.shareParser().init(this);
161+
}
158162

159163

160164
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import android.animation.Animator
44
import android.animation.ValueAnimator
55
import android.annotation.SuppressLint
66
import android.content.Context
7-
import android.graphics.drawable.Drawable
87
import android.os.Build
98
import android.util.AttributeSet
109
import android.util.Log
@@ -13,7 +12,6 @@ import android.view.View
1312
import android.view.animation.LinearInterpolator
1413
import android.widget.ImageView
1514
import com.opensource.svgaplayer.utils.SVGARange
16-
import java.lang.ref.WeakReference
1715
import java.net.URL
1816

1917
/**

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import java.net.HttpURLConnection
1111
import java.net.URL
1212
import java.security.MessageDigest
1313
import java.util.concurrent.Executors
14-
import java.util.concurrent.LinkedBlockingQueue
1514
import java.util.concurrent.ThreadPoolExecutor
16-
import java.util.concurrent.TimeUnit
1715
import java.util.zip.Inflater
1816
import java.util.zip.ZipInputStream
1917

@@ -23,13 +21,12 @@ import java.util.zip.ZipInputStream
2321

2422
private var fileLock: Int = 0
2523

26-
class SVGAParser(private val context: Context) {
24+
class SVGAParser(private var mContext: Context?) {
2725

2826
interface ParseCompletion {
2927

3028
fun onComplete(videoItem: SVGAVideoEntity)
3129
fun onError()
32-
3330
}
3431

3532
open class FileDownloader {
@@ -91,11 +88,22 @@ class SVGAParser(private val context: Context) {
9188
fun setThreadPoolExecutor(executor: ThreadPoolExecutor) {
9289
threadPoolExecutor = executor
9390
}
91+
private var mShareParser = SVGAParser(null)
92+
fun shareParser(): SVGAParser {
93+
return mShareParser
94+
}
95+
}
96+
97+
fun init(context: Context) {
98+
mContext = context
9499
}
95100

96101
fun decodeFromAssets(name: String, callback: ParseCompletion?) {
102+
if (mContext == null) {
103+
Log.e("SVGAParser", "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
104+
}
97105
try {
98-
context.assets.open(name)?.let {
106+
mContext?.assets?.open(name)?.let {
99107
this.decodeFromInputStream(it, buildCacheKey("file:///assets/$name"), callback, true)
100108
}
101109
}
@@ -176,14 +184,20 @@ class SVGAParser(private val context: Context) {
176184
}
177185

178186
private fun invokeCompleteCallback(videoItem: SVGAVideoEntity, callback: ParseCompletion?) {
179-
Handler(context.mainLooper).post {
187+
if (mContext == null) {
188+
Log.e("SVGAParser", "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
189+
}
190+
Handler(mContext?.mainLooper).post {
180191
callback?.onComplete(videoItem)
181192
}
182193
}
183194

184195
private fun invokeErrorCallback(e: java.lang.Exception, callback: ParseCompletion?) {
185196
e.printStackTrace()
186-
Handler(context.mainLooper).post {
197+
if (mContext == null) {
198+
Log.e("SVGAParser", "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
199+
}
200+
Handler(mContext?.mainLooper).post {
187201
callback?.onError()
188202
}
189203
}
@@ -193,8 +207,11 @@ class SVGAParser(private val context: Context) {
193207
}
194208

195209
private fun decodeFromCacheKey(cacheKey: String, callback: ParseCompletion?) {
210+
if (mContext == null) {
211+
Log.e("SVGAParser", "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
212+
}
196213
try {
197-
val cacheDir = File(context.cacheDir.absolutePath + "/" + cacheKey + "/")
214+
val cacheDir = File(mContext?.cacheDir?.absolutePath + "/" + cacheKey + "/")
198215
File(cacheDir, "movie.binary").takeIf { it.isFile }?.let { binaryFile ->
199216
try {
200217
FileInputStream(binaryFile).use {
@@ -249,7 +266,7 @@ class SVGAParser(private val context: Context) {
249266

250267
private fun buildCacheKey(url: URL): String = buildCacheKey(url.toString())
251268

252-
private fun buildCacheDir(cacheKey: String): File = File(context.cacheDir.absolutePath + "/" + cacheKey + "/")
269+
private fun buildCacheDir(cacheKey: String): File = File(mContext?.cacheDir?.absolutePath + "/" + cacheKey + "/")
253270

254271
private fun readAsBytes(inputStream: InputStream): ByteArray? {
255272
ByteArrayOutputStream().use { byteArrayOutputStream ->

0 commit comments

Comments
 (0)