Merge pull request #2417 from Bnyro/master

Add documentation to the seekbar preview
This commit is contained in:
Bnyro 2022-12-17 11:40:46 +01:00 committed by GitHub
commit 3b72211d48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,23 +24,16 @@ class SeekbarPreviewListener(
*/ */
override fun onScrubMove(timeBar: TimeBar, position: Long) { override fun onScrubMove(timeBar: TimeBar, position: Long) {
moving = true moving = true
val preview = getPreviewFrame(position) ?: return val previewFrame = getPreviewFrame(position) ?: return
// update the offset of the preview image view
updatePreviewX(position) updatePreviewX(position)
val request = ImageRequest.Builder(previewIv.context) val request = ImageRequest.Builder(previewIv.context)
.data(preview.previewUrl) .data(previewFrame.previewUrl)
.target { .target {
if (!moving) return@target if (!moving) return@target
val bitmap = it.toBitmap() val frame = cutOutBitmap(it.toBitmap(), previewFrame)
val heightPerFrame = bitmap.height / preview.framesPerPageY
val widthPerFrame = bitmap.width / preview.framesPerPageX
val frame = Bitmap.createBitmap(
bitmap,
preview.positionX * widthPerFrame,
preview.positionY * heightPerFrame,
widthPerFrame,
heightPerFrame
)
previewIv.setImageBitmap(frame) previewIv.setImageBitmap(frame)
previewIv.visibility = View.VISIBLE previewIv.visibility = View.VISIBLE
} }
@ -54,6 +47,7 @@ class SeekbarPreviewListener(
*/ */
override fun onScrubStop(timeBar: TimeBar, position: Long, canceled: Boolean) { override fun onScrubStop(timeBar: TimeBar, position: Long, canceled: Boolean) {
moving = false moving = false
// animate the disappearance of the preview image
previewIv.animate() previewIv.animate()
.alpha(0f) .alpha(0f)
.translationYBy(30f) .translationYBy(30f)
@ -71,8 +65,10 @@ class SeekbarPreviewListener(
*/ */
private fun getPreviewFrame(position: Long): PreviewFrame? { private fun getPreviewFrame(position: Long): PreviewFrame? {
var startPosition: Long = 0 var startPosition: Long = 0
// get the frames with the best quality
val frames = previewFrames.sortedBy { it.frameHeight }.lastOrNull() val frames = previewFrames.sortedBy { it.frameHeight }.lastOrNull()
frames?.urls?.forEach { url -> frames?.urls?.forEach { url ->
// iterate over all available positions and find the one matching the current position
for (y in 0 until frames.framesPerPageY!!) { for (y in 0 until frames.framesPerPageY!!) {
for (x in 0 until frames.framesPerPageX!!) { for (x in 0 until frames.framesPerPageX!!) {
val endPosition = startPosition + frames.durationPerFrame!!.toLong() val endPosition = startPosition + frames.durationPerFrame!!.toLong()
@ -86,6 +82,24 @@ class SeekbarPreviewListener(
return null return null
} }
/**
* Cut off a new bitmap from the image that contains multiple preview thumbnails
*/
private fun cutOutBitmap(bitmap: Bitmap, previewFrame: PreviewFrame): Bitmap {
val heightPerFrame = bitmap.height / previewFrame.framesPerPageY
val widthPerFrame = bitmap.width / previewFrame.framesPerPageX
return Bitmap.createBitmap(
bitmap,
previewFrame.positionX * widthPerFrame,
previewFrame.positionY * heightPerFrame,
widthPerFrame,
heightPerFrame
)
}
/**
* Update the offset of the preview image to fit the current scrubber position
*/
private fun updatePreviewX(position: Long) { private fun updatePreviewX(position: Long) {
val params = previewIv.layoutParams as MarginLayoutParams val params = previewIv.layoutParams as MarginLayoutParams
val parentWidth = (previewIv.parent as View).width val parentWidth = (previewIv.parent as View).width
@ -100,11 +114,18 @@ class SeekbarPreviewListener(
previewIv.layoutParams = params previewIv.layoutParams = params
} }
/**
* Normalize the offset to not overflow the screen
*/
@Suppress("SameParameterValue")
private fun normalizeOffset(offset: Int, min: Int, max: Int): Int { private fun normalizeOffset(offset: Int, min: Int, max: Int): Int {
return maxOf(min, minOf(max, offset)) return maxOf(min, minOf(max, offset))
} }
companion object { companion object {
/**
* The minimum start and end padding for the seekbar preview
*/
const val MIN_PADDING = 20 const val MIN_PADDING = 20
} }
} }