赞
踩
给Android 原生的SeekBar控件添加一个指示器标签;记录一下
按下时弹出popupwindow,进度条更新时刷新pw,松开时关闭pw;
- public class SeekBarPopUtils {
-
- private static PopupWindow popWin = null;
- private static ConstraintLayout clPopPar = null;
- private static TextView tvPopTxt = null;
-
- public static void showPop(int progress,View seekBar){
- popWin = new PopupWindow();
- View mPopView = LayoutInflater.from(BaseApp.Companion.getAppContext()).inflate(R.layout.item_popup_win,null,false);
- clPopPar = mPopView.findViewById(R.id.cl_pop_par);
- tvPopTxt = mPopView.findViewById(R.id.tv_pop_txt);
- popWin.setContentView(mPopView);
- popWin.setHeight(dp2px(30));
- popWin.setWidth(seekBar.getWidth());
- popWin.showAsDropDown(seekBar,0,-(dp2px(30) + popWin.getHeight()));
- move(progress,seekBar);
- }
-
-
- public static void move(int progress,View seekBar){
- if (clPopPar != null){
- int tvPopWidth = dp2px(40);
- ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(
- tvPopWidth, dp2px(30)
- );
- params.startToStart = clPopPar.getId();
- params.setMarginStart((seekBar.getWidth() - tvPopWidth)/100 * progress + tvPopWidth/3);
- tvPopTxt.setLayoutParams(params);
-
- tvPopTxt.setText(progress"");
- }
- }
-
- private static int dp2px(int dpVal){
- return (int) TypedValue.applyDimension(
- TypedValue.COMPLEX_UNIT_DIP,
- (float) dpVal,
- BaseApp.Companion.getAppContext().getResources().getDisplayMetrics()
- );
- }
-
- public static void dismiss(){
- popWin.dismiss();
- popWin = null;
- clPopPar = null;
- tvPopTxt = null;
- }
- }
布局:
- <?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="30dp"
- android:layout_height="40dp"
- 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="30dp"
- android:layout_height="40dp"
- android:gravity="center"
- android:paddingBottom="10px"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintBottom_toBottomOf="parent"
- android:textSize="14sp"
- android:background="@mipmap/popup_center_seekbar_qipao"
- android:textColor="#fff"
- tools:text = "55" />
-
- </androidx.constraintlayout.widget.ConstraintLayout>
使用:
- mSeekbar.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()
- }
- })
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。