Android进阶之SeekBar动态显示进度

SeekBar 在开发中并不陌生,默认的SeekBar是不显示进度的,当然用吐司或者文案在旁边实时显示也是可以的,那能不能移动的时候才显示,默认不显示呢,当然网上花哨的三方工具类太多了,但是我只是单纯的想在SeekBar的基础上去添加一个可以跟随移动显示的气泡而已~

先看一下效果:

这篇文章可能会满足你的需求

1.原生SeekBar使用,无需重写

2.改动量少,不会对控件有任何影响

3.使用灵活, Utils使用,复制粘贴即可使用

先说一下原理吧:

1.首先最最基础的就是怎么样在不做到对原有控件产生影响的情况下去显示呢?

答: PopupWindow,它只需要拿到对应的目标控件即可指定显示位置

2.如何去跟随移动呢?

答:PopupWindow本身不会动态移动,只需要在该弹窗里面设置一个控件,让该控件移动即可

具体实现

拿到控件,用PopupWindow显示在该控件附近,根据SeekBar的进度,动态设置该弹窗里面子控件的位置

使用

这里是SeekBar移动监听,在这里的三个方法加上对应的方法即可

复制代码
        mDataBind.controlVolumeSeekbar.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                //滑块移动

                SeekBarPopUtils.move(progress,seekBar!!)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
                //滑块按下
                SeekBarPopUtils.showPop(seekBar!!)
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                //滑块松开
                SeekBarPopUtils.dismiss()
            }
        })

SeekBarPopUtils 代码

注意,Kotlin写的,新建文件的时候要建Kotlin文件

复制代码
/**
 * SeekBar移动时弹出对应的气泡加数字*/
@SuppressLint("StaticFieldLeak")
object SeekBarPopUtils {

    private var popWin: PopupWindow? = null
    private var clPopPar: ConstraintLayout? = null
    private var tvPopTxt: TextView? = null

    fun showPop(seekBar: SeekBar){
        popWin = PopupWindow()
        val mPopView = LayoutInflater.from(BaseApplication.getContext()).inflate(R.layout.item_popup_win,null,false)
        clPopPar = mPopView.findViewById<ConstraintLayout>(R.id.cl_pop_par)
        tvPopTxt = mPopView.findViewById<TextView>(R.id.tv_pop_txt)
        
        popWin?.contentView = mPopView
        popWin?.height = AppHelper.dp2px(30)
        popWin?.width = seekBar.width
        popWin?.showAsDropDown(seekBar,0,-(AppHelper.dp2px(30) + popWin!!.height))
    }


    fun move(progress: Int,seekBar: SeekBar){
        val tvPopWidth = AppHelper.dp2px(40)
        val params: ConstraintLayout.LayoutParams = ConstraintLayout.LayoutParams(
            tvPopWidth, AppHelper.dp2px(30)
        )
        params.startToStart = clPopPar!!.id
        params.marginStart = (seekBar.width - tvPopWidth)/100 * progress + tvPopWidth/3
        tvPopTxt?.layoutParams = params

        tvPopTxt?.text = progress.toString()

    }


    fun dismiss(){
        popWin?.dismiss()
        popWin = null
        clPopPar = null
        tvPopTxt = null
    }

}

item_popup_win 代码

复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/cl_pop_par"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <TextView
        android:id="@+id/tv_pop_txt"
        android:layout_width="@dimen/dimen_40"
        android:layout_height="match_parent"
        android:gravity="center"
        app:layout_constraintStart_toStartOf="parent"
        android:background="@drawable/bg_ffffff_12"
        android:textSize="@dimen/sp_16"
        android:textColor="@color/black"
        tools:text = "999"/>

</androidx.constraintlayout.widget.ConstraintLayout>

AppHelper.dp2px

复制代码
    fun dp2px(dpVal: Int): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dpVal.toFloat(),
            BaseApplication.getContext().resources.displayMetrics
        ).toInt()
    }
相关推荐
JohnnyDeng944 分钟前
【Android】Hilt 依赖注入:原理与最佳实践
android·kotlin·mvvm·hilt
星间都市山脉4 小时前
Android STS(Security Test Suite)完整介绍与测试流程
android·java·linux·windows·ubuntu·android studio·androidx
Yeyu4 小时前
你真的了解AIDL吗? 附:AIDL 与 Binder 交互全解析
android
dualven_in_csdn6 小时前
一键起飞调用示例
android·java·javascript
故渊at6 小时前
第十板块:Android 系统稳定性与调试 | 第二十五篇:Watchdog 与 ANR 的系统级监控
android·watchdog·系统稳定性·anr·超时监控
故渊at7 小时前
第十板块:Android 系统稳定性与调试 | 第二十六篇:Systrace 与 Perfetto 的系统级性能分析
android·perfetto·性能分析·systrace·系统稳定性
吕工-老船长19987 小时前
20260610----S905Y5(Android14)-----连接网络自动更新时间,时间设置为24小时
android
杉氧9 小时前
Kotlin 协程深度解析④:架构实战——在 MVVM/MVI 中的进阶应用
android·kotlin
Ab_stupid9 小时前
CTF-Android培训笔记
android·笔记