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

优化线程使用 #253

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ open class SVGAImageView : ImageView {
}
}
typedArray.getString(R.styleable.SVGAImageView_source)?.let {
ParserSourceThread(this, it).start()
SVGAParser.threadPoolExecutor.execute(SyncSourceParser(this, it))
maxlee marked this conversation as resolved.
Show resolved Hide resolved
}
typedArray.recycle()
}
Expand Down Expand Up @@ -277,11 +277,11 @@ open class SVGAImageView : ImageView {
/**
* 解析资源线程,不持有外部引用
*/
private class ParserSourceThread(view: SVGAImageView, val source: String) : Thread() {
private class SyncSourceParser(view: SVGAImageView, val source: String) : Runnable {
/**
* 使用弱引用解决内存泄漏
*/
private val weakReference = WeakReference<SVGAImageView>(view)
private val weakReference = WeakReference(view)
private val parser = SVGAParser(view.context)

override fun run() {
Expand All @@ -301,7 +301,7 @@ open class SVGAImageView : ImageView {
override fun onError() {}
}
}
} // end of ParserSourceThread
} // end of SyncSourceParser

private class AnimatorListener(view: SVGAImageView) : Animator.AnimatorListener {
private val weakReference = WeakReference<SVGAImageView>(view)
Expand Down
8 changes: 6 additions & 2 deletions library/src/main/java/com/opensource/svgaplayer/SVGAParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.opensource.svgaplayer
import android.content.Context
import android.net.http.HttpResponseCache
import android.os.Handler
import android.os.Looper
import android.util.Log
import com.opensource.svgaplayer.proto.MovieEntity
import org.json.JSONObject
Expand Down Expand Up @@ -91,6 +92,7 @@ class SVGAParser(context: Context?) {
threadPoolExecutor = executor
}
private var mShareParser = SVGAParser(null)
private val mMainHandler = Handler(Looper.getMainLooper())
fun shareParser(): SVGAParser {
return mShareParser
}
Expand Down Expand Up @@ -189,7 +191,8 @@ class SVGAParser(context: Context?) {
if (mContextRef.get() == null) {
Log.e("SVGAParser", "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
}
Handler(mContextRef.get()?.mainLooper).post {

mMainHandler.post {
callback?.onComplete(videoItem)
}
}
Expand All @@ -199,7 +202,8 @@ class SVGAParser(context: Context?) {
if (mContextRef.get() == null) {
Log.e("SVGAParser", "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
}
Handler(mContextRef.get()?.mainLooper).post {

mMainHandler.post {
callback?.onError()
}
}
Expand Down