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

Commit

Permalink
fix: Memory issue, due to android.view.ImageView drawable cycle refer…
Browse files Browse the repository at this point in the history
…ence, let drawable sets to WeakReference if ImageView detached.
  • Loading branch information
PonyCui committed Jan 16, 2019
1 parent 7f0b128 commit d040e36
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
15 changes: 15 additions & 0 deletions library/src/main/java/com/opensource/svgaplayer/SVGAImageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.util.Log
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import java.lang.ref.WeakReference
import java.net.URL

/**
Expand Down Expand Up @@ -111,13 +112,27 @@ open class SVGAImageView : ImageView {
}
}

private var detachedDrawable: WeakReference<Drawable>? = null

override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
this.drawable?.let {
this.detachedDrawable = WeakReference(it)
}
this.setImageDrawable(null)
animator?.cancel()
animator?.removeAllListeners()
animator?.removeAllUpdateListeners()
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()
this.detachedDrawable?.get()?.let {
this.setImageDrawable(it)
this.detachedDrawable = null
}
}

private fun loadAttrs(attrs: AttributeSet) {
val typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.SVGAImageView, 0, 0)
loops = typedArray.getInt(R.styleable.SVGAImageView_loopCount, 0)
Expand Down
17 changes: 2 additions & 15 deletions library/src/main/java/com/opensource/svgaplayer/SVGAVideoEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,13 @@ class SVGAVideoEntity {
private fun resetAudios(obj: MovieEntity, completionBlock: () -> Unit) {
obj.audios?.takeIf { it.isNotEmpty() }?.let { audios ->
var soundLoaded = 0
// val soundPool = SoundPool(Math.min(12, audios.count()), AudioManager.STREAM_RING, 0)
//5.0以上版本建议使用builder方式创建SoundPool,在9.0以下还未发现使用new SoundPool有什么问题,9.0很多机型继续使用new SoundPool方式部分room已无效。
val soundPool = if (android.os.Build.VERSION.SDK_INT >= 21) {
SoundPool.Builder().setAudioAttributes(AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build())
.setMaxStreams(Math.min(12, audios.count()))
.build()
} else
} else {
SoundPool(Math.min(12, audios.count()), AudioManager.STREAM_MUSIC, 0)

}
val audiosFile = HashMap<String, File>()
soundPool.setOnLoadCompleteListener { soundPool, _, _ ->
soundLoaded++
Expand All @@ -185,7 +183,6 @@ class SVGAVideoEntity {
}
}
if (audiosData.count() > 0) {

audiosData.forEach {
val tmpFile = File.createTempFile(it.key, ".mp3")
val fos = FileOutputStream(tmpFile)
Expand All @@ -194,16 +191,6 @@ class SVGAVideoEntity {
fos.close()
audiosFile[it.key] = tmpFile
}

//Call requires API level 24 (current min is 14): java.util.HashMap#forEach
/* audiosData.forEach { aKey, bytes ->
val tmpFile = File.createTempFile(aKey, ".mp3")
val fos = FileOutputStream(tmpFile)
fos.write(bytes)
fos.flush()
fos.close()
audiosFile[aKey] = tmpFile
}*/
}
this.audios = audios.map { audio ->
val item = SVGAAudioEntity(audio)
Expand Down

0 comments on commit d040e36

Please sign in to comment.