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

Commit d040e36

Browse files
committed
fix: Memory issue, due to android.view.ImageView drawable cycle reference, let drawable sets to WeakReference if ImageView detached.
1 parent 7f0b128 commit d040e36

2 files changed

Lines changed: 17 additions & 15 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import android.util.Log
1313
import android.view.View
1414
import android.view.animation.LinearInterpolator
1515
import android.widget.ImageView
16+
import java.lang.ref.WeakReference
1617
import java.net.URL
1718

1819
/**
@@ -111,13 +112,27 @@ open class SVGAImageView : ImageView {
111112
}
112113
}
113114

115+
private var detachedDrawable: WeakReference<Drawable>? = null
116+
114117
override fun onDetachedFromWindow() {
115118
super.onDetachedFromWindow()
119+
this.drawable?.let {
120+
this.detachedDrawable = WeakReference(it)
121+
}
122+
this.setImageDrawable(null)
116123
animator?.cancel()
117124
animator?.removeAllListeners()
118125
animator?.removeAllUpdateListeners()
119126
}
120127

128+
override fun onAttachedToWindow() {
129+
super.onAttachedToWindow()
130+
this.detachedDrawable?.get()?.let {
131+
this.setImageDrawable(it)
132+
this.detachedDrawable = null
133+
}
134+
}
135+
121136
private fun loadAttrs(attrs: AttributeSet) {
122137
val typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.SVGAImageView, 0, 0)
123138
loops = typedArray.getInt(R.styleable.SVGAImageView_loopCount, 0)

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,13 @@ class SVGAVideoEntity {
156156
private fun resetAudios(obj: MovieEntity, completionBlock: () -> Unit) {
157157
obj.audios?.takeIf { it.isNotEmpty() }?.let { audios ->
158158
var soundLoaded = 0
159-
// val soundPool = SoundPool(Math.min(12, audios.count()), AudioManager.STREAM_RING, 0)
160-
//5.0以上版本建议使用builder方式创建SoundPool,在9.0以下还未发现使用new SoundPool有什么问题,9.0很多机型继续使用new SoundPool方式部分room已无效。
161159
val soundPool = if (android.os.Build.VERSION.SDK_INT >= 21) {
162160
SoundPool.Builder().setAudioAttributes(AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build())
163161
.setMaxStreams(Math.min(12, audios.count()))
164162
.build()
165-
} else
163+
} else {
166164
SoundPool(Math.min(12, audios.count()), AudioManager.STREAM_MUSIC, 0)
167-
165+
}
168166
val audiosFile = HashMap<String, File>()
169167
soundPool.setOnLoadCompleteListener { soundPool, _, _ ->
170168
soundLoaded++
@@ -185,7 +183,6 @@ class SVGAVideoEntity {
185183
}
186184
}
187185
if (audiosData.count() > 0) {
188-
189186
audiosData.forEach {
190187
val tmpFile = File.createTempFile(it.key, ".mp3")
191188
val fos = FileOutputStream(tmpFile)
@@ -194,16 +191,6 @@ class SVGAVideoEntity {
194191
fos.close()
195192
audiosFile[it.key] = tmpFile
196193
}
197-
198-
//Call requires API level 24 (current min is 14): java.util.HashMap#forEach
199-
/* audiosData.forEach { aKey, bytes ->
200-
val tmpFile = File.createTempFile(aKey, ".mp3")
201-
val fos = FileOutputStream(tmpFile)
202-
fos.write(bytes)
203-
fos.flush()
204-
fos.close()
205-
audiosFile[aKey] = tmpFile
206-
}*/
207194
}
208195
this.audios = audios.map { audio ->
209196
val item = SVGAAudioEntity(audio)

0 commit comments

Comments
 (0)