Improve the responsiveness of the timebar preview

This commit is contained in:
Bnyro 2023-01-08 17:39:38 +01:00
parent b92bfa87ed
commit c4a2d91fd4
3 changed files with 49 additions and 38 deletions

View File

@ -1,6 +1,5 @@
package com.github.libretube.ui.extensions
import android.util.Log
import com.github.libretube.R
import com.github.libretube.util.PreferenceHelper
import com.google.android.material.button.MaterialButton

View File

@ -17,29 +17,19 @@ class SeekbarPreviewListener(
) : TimeBar.OnScrubListener {
private var moving = false
override fun onScrubStart(timeBar: TimeBar, position: Long) {}
override fun onScrubStart(timeBar: TimeBar, position: Long) {
moving = true
processPreview(position)
}
/**
* Show a preview of the scrubber position
*/
override fun onScrubMove(timeBar: TimeBar, position: Long) {
moving = true
val previewFrame = getPreviewFrame(position) ?: return
// update the offset of the preview image view
updatePreviewX(position)
val request = ImageRequest.Builder(previewIv.context)
.data(previewFrame.previewUrl)
.target {
if (!moving) return@target
val frame = cutOutBitmap(it.toBitmap(), previewFrame)
previewIv.setImageBitmap(frame)
previewIv.visibility = View.VISIBLE
}
.build()
ImageHelper.imageLoader.enqueue(request)
processPreview(position)
}
/**
@ -60,6 +50,28 @@ class SeekbarPreviewListener(
.start()
}
/**
* Make a request to get the image frame and update its position
*/
private fun processPreview(position: Long) {
val previewFrame = getPreviewFrame(position) ?: return
// update the offset of the preview image view
updatePreviewX(position)
val request = ImageRequest.Builder(previewIv.context)
.data(previewFrame.previewUrl)
.target {
if (!moving) return@target
val frame = cutOutBitmap(it.toBitmap(), previewFrame)
previewIv.setImageBitmap(frame)
previewIv.visibility = View.VISIBLE
}
.build()
ImageHelper.imageLoader.enqueue(request)
}
/**
* Get the preview frame according to the current position
*/