进度条

This commit is contained in:
renhaoting 2025-11-19 14:15:26 +08:00
parent c5153e3a95
commit e4dc667e7a
3 changed files with 89 additions and 0 deletions

View File

@ -32,6 +32,8 @@ class HomeItemFragment : AppViewsEmptyViewModelFragment<ViewBinding>() {
private var mPlayer: YouTubePlayer? = null
private var mVideoData: YoutubeVideo? = null
private var mIsPlaying: Boolean = false
private var mCurPlayedSecond: Float = 0F
private var mTotalDuration: Float = -1F
override fun inflateViewBinding(
@ -199,6 +201,17 @@ class HomeItemFragment : AppViewsEmptyViewModelFragment<ViewBinding>() {
}
}
override fun onCurrentSecond(youTubePlayer: YouTubePlayer, second: Float) {
super.onCurrentSecond(youTubePlayer, second)
mCurPlayedSecond = second
updateProgressbar()
}
override fun onVideoDuration(youTubePlayer: YouTubePlayer, duration: Float) {
super.onVideoDuration(youTubePlayer, duration)
mTotalDuration = duration
}
override fun onStateChange(
youTubePlayer: YouTubePlayer,
state: PlayerConstants.PlayerState
@ -242,6 +255,16 @@ class HomeItemFragment : AppViewsEmptyViewModelFragment<ViewBinding>() {
mPlayer?.pause()
}
private fun updateProgressbar() {
with(binding?.progressBarPlayer!!) {
setMax(100)
if (mTotalDuration > 0) {
setProgress((100*mCurPlayedSecond/mTotalDuration).toInt())
}
}
}
private fun hidePlayIconAnim() {
if (!binding?.playIcon!!.isVisible) {
return

View File

@ -155,4 +155,21 @@
</LinearLayout>
<!--<ProgressBar
android:id="@+id/progress_bar_player"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_marginBottom="3dp"
android:layout_gravity="bottom"
/>-->
<com.ama.core.architecture.widget.CustomProgressBar
android:id="@+id/progress_bar_player"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_marginBottom="6dp"
android:layout_marginHorizontal="15dp"
android:layout_gravity="bottom"
/>
</FrameLayout>

View File

@ -0,0 +1,49 @@
package com.ama.core.architecture.widget
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
class CustomProgressBar(context: Context, attrs: AttributeSet) : View(context, attrs) {
private var progress = 0
private var max = 100
private val grayColor = Color.parseColor("#1affffff")
private val greenColor = Color.parseColor("#ff00ff09")
private val paint = Paint().apply {
isAntiAlias = true
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
val width = measuredWidth.toFloat()
val height = measuredHeight.toFloat()
paint.color = grayColor
canvas.drawRoundRect(0f, 0f, width, height, width/2, 0F, paint)
val progressWidth = (width) * progress / max
if (progressWidth > 0) {
paint.color = greenColor
canvas.drawRoundRect(0f, 0f, progressWidth, height, width/2, 0F, paint)
}
}
fun setProgress(progress: Int) {
this.progress = progress.coerceIn(0, max)
invalidate()
}
fun setMax(max: Int) {
this.max = max
invalidate()
}
}