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

bug: 修改svga图片缩放模式 #449

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
35 changes: 21 additions & 14 deletions library/src/main/java/com/opensource/svgaplayer/SVGAVideoEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,14 @@ internal abstract class SVGABitmapDecoder<T> {
}
}

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?
}