From d12faea0f7dda6711552a8a5e8befcd796d18f44 Mon Sep 17 00:00:00 2001 From: tangxiaolu Date: Thu, 29 Sep 2022 17:23:54 +0800 Subject: [PATCH] =?UTF-8?q?bug:=20=E4=BF=AE=E6=94=B9svga=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E7=BC=A9=E6=94=BE=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../opensource/svgaplayer/SVGAVideoEntity.kt | 35 +++++++++++-------- .../bitmap/BitmapSampleSizeCalculator.kt | 10 ++++++ .../svgaplayer/bitmap/SVGABitmapDecoder.kt | 9 +++++ 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/library/src/main/java/com/opensource/svgaplayer/SVGAVideoEntity.kt b/library/src/main/java/com/opensource/svgaplayer/SVGAVideoEntity.kt index 49cb8302..316e9be3 100644 --- a/library/src/main/java/com/opensource/svgaplayer/SVGAVideoEntity.kt +++ b/library/src/main/java/com/opensource/svgaplayer/SVGAVideoEntity.kt @@ -5,6 +5,7 @@ import android.media.AudioAttributes import android.media.AudioManager import android.media.SoundPool import android.os.Build +import com.opensource.svgaplayer.bitmap.BitmapSampleSizeCalculator import com.opensource.svgaplayer.bitmap.SVGABitmapByteArrayDecoder import com.opensource.svgaplayer.bitmap.SVGABitmapFileDecoder import com.opensource.svgaplayer.entities.SVGAAudioEntity @@ -48,6 +49,10 @@ class SVGAVideoEntity { private var mCacheDir: File private var mFrameHeight = 0 private var mFrameWidth = 0 + /** + * 图片采样率 + * */ + private var imageSampleSize = 1 private var mPlayCallback: SVGAParser.PlayCallback?=null private lateinit var mCallback: () -> Unit @@ -60,6 +65,7 @@ class SVGAVideoEntity { val movieJsonObject = json.optJSONObject("movie") ?: return setupByJson(movieJsonObject) try { + imageSampleSize = BitmapSampleSizeCalculator.calculate(videoSize.height.toInt(),videoSize.width.toInt(),mFrameWidth,mFrameHeight) parserImages(json) } catch (e: Exception) { e.printStackTrace() @@ -88,6 +94,7 @@ class SVGAVideoEntity { this.movieItem = entity entity.params?.let(this::setupByMovie) try { + imageSampleSize = BitmapSampleSizeCalculator.calculate(videoSize.height.toInt(),videoSize.width.toInt(),mFrameWidth,mFrameHeight) parserImages(entity) } catch (e: Exception) { e.printStackTrace() @@ -146,7 +153,7 @@ class SVGAVideoEntity { } private fun createBitmap(filePath: String): Bitmap? { - return SVGABitmapFileDecoder.decodeBitmapFrom(filePath, mFrameWidth, mFrameHeight) + return SVGABitmapFileDecoder.decodeBitmapFrom(filePath, imageSampleSize) } private fun parserImages(obj: MovieEntity) { @@ -167,7 +174,7 @@ class SVGAVideoEntity { } private fun createBitmap(byteArray: ByteArray, filePath: String): Bitmap? { - val bitmap = SVGABitmapByteArrayDecoder.decodeBitmapFrom(byteArray, mFrameWidth, mFrameHeight) + val bitmap = SVGABitmapByteArrayDecoder.decodeBitmapFrom(byteArray, imageSampleSize) return bitmap ?: createBitmap(filePath) } @@ -232,10 +239,10 @@ class SVGAVideoEntity { val offset = ((startTime / totalTime) * length).toLong() if (SVGASoundManager.isInit()) { item.soundID = SVGASoundManager.load(soundCallback, - it.fd, - offset, - length.toLong(), - 1) + it.fd, + offset, + length.toLong(), + 1) } else { item.soundID = soundPool?.load(it.fd, offset, length.toLong(), 1) } @@ -257,10 +264,10 @@ class SVGAVideoEntity { audiosDataMap.forEach { val audioCache = SVGACache.buildAudioFile(it.key) audiosFileMap[it.key] = - audioCache.takeIf { file -> file.exists() } ?: generateAudioFile( - audioCache, - it.value - ) + audioCache.takeIf { file -> file.exists() } ?: generateAudioFile( + audioCache, + it.value + ) } } return audiosFileMap @@ -316,11 +323,11 @@ class SVGAVideoEntity { return try { if (Build.VERSION.SDK_INT >= 21) { val attributes = AudioAttributes.Builder() - .setUsage(AudioAttributes.USAGE_MEDIA) - .build() + .setUsage(AudioAttributes.USAGE_MEDIA) + .build() SoundPool.Builder().setAudioAttributes(attributes) - .setMaxStreams(12.coerceAtMost(entity.audios.count())) - .build() + .setMaxStreams(12.coerceAtMost(entity.audios.count())) + .build() } else { SoundPool(12.coerceAtMost(entity.audios.count()), AudioManager.STREAM_MUSIC, 0) } diff --git a/library/src/main/java/com/opensource/svgaplayer/bitmap/BitmapSampleSizeCalculator.kt b/library/src/main/java/com/opensource/svgaplayer/bitmap/BitmapSampleSizeCalculator.kt index e1ea015e..69112fab 100644 --- a/library/src/main/java/com/opensource/svgaplayer/bitmap/BitmapSampleSizeCalculator.kt +++ b/library/src/main/java/com/opensource/svgaplayer/bitmap/BitmapSampleSizeCalculator.kt @@ -8,9 +8,19 @@ import android.graphics.BitmapFactory */ internal object BitmapSampleSizeCalculator { + /** + * 根据BitmapFactory.Options 与期望宽高计算采样率 + * */ fun calculate(options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int { // Raw height and width of image val (height: Int, width: Int) = options.run { outHeight to outWidth } + return calculate(height, width, reqWidth, reqHeight) + } + + /** + * 根据预期显示的宽高 与实际宽高计算采样率 + * */ + fun calculate(height: Int, width: Int, reqWidth: Int, reqHeight: Int): Int { var inSampleSize = 1 if (reqHeight <= 0 || reqWidth <= 0) { diff --git a/library/src/main/java/com/opensource/svgaplayer/bitmap/SVGABitmapDecoder.kt b/library/src/main/java/com/opensource/svgaplayer/bitmap/SVGABitmapDecoder.kt index 8816fbb0..361bf4b8 100644 --- a/library/src/main/java/com/opensource/svgaplayer/bitmap/SVGABitmapDecoder.kt +++ b/library/src/main/java/com/opensource/svgaplayer/bitmap/SVGABitmapDecoder.kt @@ -31,5 +31,14 @@ internal abstract class SVGABitmapDecoder { } } + fun decodeBitmapFrom(data: T, sampleSize:Int): Bitmap? = BitmapFactory.Options().run { + // 如果期望的宽高是合法的, 则开启检测尺寸模式 + inPreferredConfig = Bitmap.Config.RGB_565 + inJustDecodeBounds = false + // Calculate inSampleSize + inSampleSize = sampleSize + return onDecode(data, this) + } + abstract fun onDecode(data: T, ops: BitmapFactory.Options): Bitmap? } \ No newline at end of file