优化百分比拉动seekbar控件
This commit is contained in:
		@@ -76,6 +76,6 @@ dependencies {
 | 
			
		||||
    implementation 'androidx.fragment:fragment:1.1.0'
 | 
			
		||||
    implementation 'com.google.android.material:material:1.4.0'
 | 
			
		||||
    
 | 
			
		||||
    implementation 'cc.winboll.studio:libappbase:2.1.3'
 | 
			
		||||
    implementation 'cc.winboll.studio:libapputils:9.4.2'
 | 
			
		||||
    implementation 'cc.winboll.studio:libappbase:2.1.5'
 | 
			
		||||
    implementation 'cc.winboll.studio:libapputils:9.4.4'
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,8 @@
 | 
			
		||||
#Created by .winboll/winboll_app_build.gradle
 | 
			
		||||
#Sat Mar 08 04:21:32 HKT 2025
 | 
			
		||||
#Sun Mar 09 03:25:41 GMT 2025
 | 
			
		||||
stageCount=7
 | 
			
		||||
libraryProject=libaes
 | 
			
		||||
baseVersion=7.6
 | 
			
		||||
publishVersion=7.6.6
 | 
			
		||||
buildCount=0
 | 
			
		||||
buildCount=66
 | 
			
		||||
baseBetaVersion=7.6.7
 | 
			
		||||
 
 | 
			
		||||
@@ -51,6 +51,6 @@ dependencies {
 | 
			
		||||
    implementation 'androidx.fragment:fragment:1.1.0'
 | 
			
		||||
    implementation 'com.google.android.material:material:1.4.0'
 | 
			
		||||
    
 | 
			
		||||
    implementation 'cc.winboll.studio:libappbase:2.1.3'
 | 
			
		||||
    implementation 'cc.winboll.studio:libapputils:9.4.2'
 | 
			
		||||
    implementation 'cc.winboll.studio:libappbase:2.1.5'
 | 
			
		||||
    implementation 'cc.winboll.studio:libapputils:9.4.4'
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,8 @@
 | 
			
		||||
#Created by .winboll/winboll_app_build.gradle
 | 
			
		||||
#Sat Mar 08 04:21:16 HKT 2025
 | 
			
		||||
#Sun Mar 09 03:25:41 GMT 2025
 | 
			
		||||
stageCount=7
 | 
			
		||||
libraryProject=libaes
 | 
			
		||||
baseVersion=7.6
 | 
			
		||||
publishVersion=7.6.6
 | 
			
		||||
buildCount=0
 | 
			
		||||
buildCount=66
 | 
			
		||||
baseBetaVersion=7.6.7
 | 
			
		||||
 
 | 
			
		||||
@@ -19,11 +19,14 @@ import cc.winboll.studio.libaes.R;
 | 
			
		||||
import cc.winboll.studio.libaes.views.AOHPCTCSeekBar;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import cc.winboll.studio.libappbase.LogView;
 | 
			
		||||
 | 
			
		||||
public class TestViewPageFragment extends Fragment implements ViewPager.OnPageChangeListener, View.OnClickListener {
 | 
			
		||||
 | 
			
		||||
    public static final String TAG = "TestViewPageFragment";
 | 
			
		||||
    
 | 
			
		||||
    LogView mLogView;
 | 
			
		||||
 | 
			
		||||
    private ViewPager viewPager;
 | 
			
		||||
    private List<View> views; //用来存放放进ViewPager里面的布局
 | 
			
		||||
    //实例化存储imageView(导航原点)的集合
 | 
			
		||||
@@ -37,6 +40,9 @@ public class TestViewPageFragment extends Fragment implements ViewPager.OnPageCh
 | 
			
		||||
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 | 
			
		||||
        mView = inflater.inflate(R.layout.fragment_viewpage, container, false);
 | 
			
		||||
        
 | 
			
		||||
        mLogView = mView.findViewById(R.id.logview);
 | 
			
		||||
        mLogView.start();
 | 
			
		||||
 | 
			
		||||
        //viewPager = findViewById(R.id.activitymainViewPager1);
 | 
			
		||||
        initData();
 | 
			
		||||
        initView();//调用初始化视图方法
 | 
			
		||||
@@ -60,6 +66,7 @@ public class TestViewPageFragment extends Fragment implements ViewPager.OnPageCh
 | 
			
		||||
        initPoint();//初始化页面下方的点
 | 
			
		||||
        viewPager.setOnPageChangeListener(this);
 | 
			
		||||
        initAOHPCTCSeekBar();
 | 
			
		||||
        initAOHPCTCSeekBar2();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //初始化所要显示的布局
 | 
			
		||||
@@ -187,7 +194,9 @@ public class TestViewPageFragment extends Fragment implements ViewPager.OnPageCh
 | 
			
		||||
    void initAOHPCTCSeekBar() {
 | 
			
		||||
        AOHPCTCSeekBar seekbar = mView.findViewById(R.id.fragmentviewpageAOHPCTCSeekBar1);
 | 
			
		||||
        seekbar.setThumb(getActivity().getDrawable(R.drawable.ic_launcher));
 | 
			
		||||
        seekbar.setThumbOffset(10);
 | 
			
		||||
        //seekbar.setThumbOffset(200);
 | 
			
		||||
        //seekbar.setThumbOffset(1);
 | 
			
		||||
        seekbar.setBlurRightDP(50);
 | 
			
		||||
        seekbar.setOnOHPCListener(new AOHPCTCSeekBar.OnOHPCListener() {
 | 
			
		||||
 | 
			
		||||
                @Override
 | 
			
		||||
@@ -196,4 +205,19 @@ public class TestViewPageFragment extends Fragment implements ViewPager.OnPageCh
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    void initAOHPCTCSeekBar2() {
 | 
			
		||||
        AOHPCTCSeekBar seekbar = mView.findViewById(R.id.fragmentviewpageAOHPCTCSeekBar2);
 | 
			
		||||
        seekbar.setThumb(getActivity().getDrawable(R.drawable.ic_call));
 | 
			
		||||
        //seekbar.setThumbOffset(200);
 | 
			
		||||
        //seekbar.setThumbOffset(1);
 | 
			
		||||
        seekbar.setBlurRightDP(50);
 | 
			
		||||
        seekbar.setOnOHPCListener(new AOHPCTCSeekBar.OnOHPCListener() {
 | 
			
		||||
 | 
			
		||||
                @Override
 | 
			
		||||
                public void onOHPCommit() {
 | 
			
		||||
                    Toast.makeText(getActivity(), "onOHPCommit 2", Toast.LENGTH_SHORT).show();
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -6,18 +6,28 @@ package cc.winboll.studio.libaes.views;
 | 
			
		||||
 * @Describe AOneHundredPercantClickToCommitSeekBar
 | 
			
		||||
 */
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
import android.graphics.drawable.Drawable;
 | 
			
		||||
import android.util.AttributeSet;
 | 
			
		||||
import android.view.MotionEvent;
 | 
			
		||||
import android.widget.SeekBar;
 | 
			
		||||
import cc.winboll.studio.libappbase.LogUtils;
 | 
			
		||||
 | 
			
		||||
public class AOHPCTCSeekBar extends SeekBar {
 | 
			
		||||
 | 
			
		||||
    public static final String TAG = "AOHPCTCSeekBar";
 | 
			
		||||
 | 
			
		||||
    Context mContext;
 | 
			
		||||
 | 
			
		||||
    int thumbWidth = 1;
 | 
			
		||||
    int progressBarWidth = 1;
 | 
			
		||||
    // 设置按钮模糊右边边缘像素
 | 
			
		||||
    int blurRightDP = 1;
 | 
			
		||||
 | 
			
		||||
    // 可开始拉动的起始位置(百分比值)
 | 
			
		||||
    static final int ENABLE_POST_PERCENT_X = 20;
 | 
			
		||||
    //static final int ENABLE_POST_PERCENT_X = 20;
 | 
			
		||||
    //int seekablePosition;
 | 
			
		||||
    // 最小拉动值,滑块拉动值要超过这个值,确定事件才会提交。
 | 
			
		||||
    static final int TO_MIN_VALUE = 15;
 | 
			
		||||
    //static final int TO_MIN_VALUE = 15;
 | 
			
		||||
    // 外部接口对象,确定事件提交会调用该对象的方法
 | 
			
		||||
    OnOHPCListener mOnOHPCListener;
 | 
			
		||||
    // 是否从起点拉动的标志
 | 
			
		||||
@@ -25,6 +35,10 @@ public class AOHPCTCSeekBar extends SeekBar {
 | 
			
		||||
    // 拉动的滑动值
 | 
			
		||||
    int mnTo = 0;
 | 
			
		||||
 | 
			
		||||
    public void setBlurRightDP(int blurRight) {
 | 
			
		||||
        this.blurRightDP = blurRight;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public void setOnOHPCListener(OnOHPCListener listener) {
 | 
			
		||||
        mOnOHPCListener = listener;
 | 
			
		||||
    }
 | 
			
		||||
@@ -35,10 +49,12 @@ public class AOHPCTCSeekBar extends SeekBar {
 | 
			
		||||
 | 
			
		||||
    public AOHPCTCSeekBar(Context context) {
 | 
			
		||||
        super(context);
 | 
			
		||||
        initView(context);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public AOHPCTCSeekBar(Context context, AttributeSet attrs) {
 | 
			
		||||
        super(context, attrs);
 | 
			
		||||
        initView(context);
 | 
			
		||||
 | 
			
		||||
        //LogUtils.d(TAG, "AOHPCTCSeekBar(...)");
 | 
			
		||||
 | 
			
		||||
@@ -61,12 +77,23 @@ public class AOHPCTCSeekBar extends SeekBar {
 | 
			
		||||
 | 
			
		||||
    public AOHPCTCSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
 | 
			
		||||
        super(context, attrs, defStyleAttr);
 | 
			
		||||
        initView(context);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void initView(Context context) {
 | 
			
		||||
        LogUtils.d(TAG, "initView(...)");
 | 
			
		||||
        mContext = context;
 | 
			
		||||
//        Drawable thumbDrawable = getThumb();
 | 
			
		||||
//        if (thumbDrawable!= null) {
 | 
			
		||||
//            int iconWidth = thumbDrawable.getIntrinsicWidth();
 | 
			
		||||
//            LogUtils.d(TAG, String.format("iconWidth %d", iconWidth));
 | 
			
		||||
//            seekablePosition = iconWidth;
 | 
			
		||||
//        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void onFinishInflate() {
 | 
			
		||||
        super.onFinishInflate();
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@@ -74,32 +101,51 @@ public class AOHPCTCSeekBar extends SeekBar {
 | 
			
		||||
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
 | 
			
		||||
            //LogUtils.d(TAG, "ACTION_DOWN");
 | 
			
		||||
            // 有效的拖动起始位置(ENABLE_POST_PERCENT_X)%
 | 
			
		||||
            int nEnablePostX = ((getRight() - getLeft()) * ENABLE_POST_PERCENT_X / 100) + getLeft();
 | 
			
		||||
 | 
			
		||||
            if ((getLeft() < event.getX())
 | 
			
		||||
                && (event.getX() < nEnablePostX)) {
 | 
			
		||||
                //LogUtils.d(TAG, "event.getX() is " + Float.toString(event.getX()));
 | 
			
		||||
            //int nEnablePostX = ((getRight() - getLeft()) * ENABLE_POST_PERCENT_X / 100) + getLeft();
 | 
			
		||||
            //int nEnablePostX = ((getRight() - getLeft()) * seekablePosition / 100) + getLeft();
 | 
			
		||||
            /*LogUtils.d(TAG, "event.getX() is " + Float.toString(event.getX()));
 | 
			
		||||
            LogUtils.d(TAG, String.format("thumbWidth %d progressBarWidth %d", thumbWidth, progressBarWidth));
 | 
			
		||||
            LogUtils.d(TAG, String.format("mIsStartTo %s", mIsStartTo));
 | 
			
		||||
            */
 | 
			
		||||
            if (thumbWidth + blurRightDP > event.getX() && event.getX() > 0) {
 | 
			
		||||
                mIsStartTo = true;
 | 
			
		||||
                return super.dispatchTouchEvent(event);
 | 
			
		||||
            }
 | 
			
		||||
            if (!mIsStartTo) {
 | 
			
		||||
                resetView();
 | 
			
		||||
                return false;
 | 
			
		||||
                return true;
 | 
			
		||||
                //return super.dispatchTouchEvent(event);
 | 
			
		||||
            }
 | 
			
		||||
//            if (!mIsStartTo) {
 | 
			
		||||
//                resetView();
 | 
			
		||||
//                return false;
 | 
			
		||||
//            }
 | 
			
		||||
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
 | 
			
		||||
            //LogUtils.d(TAG, "ACTION_MOVE");
 | 
			
		||||
            /*LogUtils.d(TAG, "event.getX() is " + Float.toString(event.getX()));
 | 
			
		||||
            LogUtils.d(TAG, String.format("thumbWidth %d progressBarWidth %d", thumbWidth, progressBarWidth));
 | 
			
		||||
            LogUtils.d(TAG, String.format("mIsStartTo %s", mIsStartTo));
 | 
			
		||||
            */
 | 
			
		||||
            if (mIsStartTo) {
 | 
			
		||||
                mnTo++;
 | 
			
		||||
                return super.dispatchTouchEvent(event);
 | 
			
		||||
            } else {
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
 | 
			
		||||
        } else if (event.getAction() == MotionEvent.ACTION_UP 
 | 
			
		||||
                   || event.getAction() == MotionEvent.ACTION_CANCEL) {
 | 
			
		||||
            //LogUtils.d(TAG, Integer.toString(getProgress()));
 | 
			
		||||
            // 提交100%确定事件
 | 
			
		||||
            if ((getProgress() == 100) && (mnTo > TO_MIN_VALUE)) {
 | 
			
		||||
                //LogUtils.d(TAG, "Commit mnTo is " + Integer.toString(mnTo));
 | 
			
		||||
//            if (getProgress() == progressBarWidth) {
 | 
			
		||||
//                //((getProgress() == 100) && (mnTo > TO_MIN_VALUE)) {
 | 
			
		||||
//                //LogUtils.d(TAG, "Commit mnTo is " + Integer.toString(mnTo));
 | 
			
		||||
//                mOnOHPCListener.onOHPCommit();
 | 
			
		||||
//                resetView();
 | 
			
		||||
//                //return true;
 | 
			
		||||
//            } else {
 | 
			
		||||
//                resetView();
 | 
			
		||||
//                mIsStartTo = false;
 | 
			
		||||
//            }
 | 
			
		||||
//
 | 
			
		||||
              if (getProgress() == progressBarWidth) {
 | 
			
		||||
                  mOnOHPCListener.onOHPCommit();
 | 
			
		||||
                //resetView();
 | 
			
		||||
                //return true;
 | 
			
		||||
              }
 | 
			
		||||
              mIsStartTo = false;
 | 
			
		||||
              resetView();
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
@@ -114,4 +160,31 @@ public class AOHPCTCSeekBar extends SeekBar {
 | 
			
		||||
        mnTo = 0;
 | 
			
		||||
        mIsStartTo = false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 | 
			
		||||
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 | 
			
		||||
        int width = MeasureSpec.getSize(widthMeasureSpec);
 | 
			
		||||
        int height = MeasureSpec.getSize(heightMeasureSpec);
 | 
			
		||||
        LogUtils.d(TAG, String.format("width %d height %d", width, height));
 | 
			
		||||
 | 
			
		||||
        // 使用width和height进行后续操作
 | 
			
		||||
 | 
			
		||||
        // 获取SeekBar的图标
 | 
			
		||||
        Drawable thumbDrawable = getThumb();
 | 
			
		||||
        if (thumbDrawable != null) {
 | 
			
		||||
            // 获取图标宽度
 | 
			
		||||
            thumbWidth = thumbDrawable.getIntrinsicWidth();
 | 
			
		||||
            // 获取进度条宽度
 | 
			
		||||
            progressBarWidth = width;//getWidth();// - getPaddingLeft() - getPaddingRight();
 | 
			
		||||
            // 计算百分比
 | 
			
		||||
            //float percentage = (float) thumbWidth / progressBarWidth * 100;
 | 
			
		||||
            LogUtils.d(TAG, String.format("thumbWidth %d progressBarWidth %d", thumbWidth, progressBarWidth));
 | 
			
		||||
            //LogUtils.d(TAG, String.format("Thumb width / ProgressBar width percentage: %f", percentage));
 | 
			
		||||
 | 
			
		||||
            //seekablePosition = (int)percentage;
 | 
			
		||||
            setThumbOffset(0);
 | 
			
		||||
            setMax(progressBarWidth);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										11
									
								
								libaes/src/main/res/drawable/ic_call.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								libaes/src/main/res/drawable/ic_call.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
<?xml version="1.0" encoding="utf-8"?>
 | 
			
		||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
 | 
			
		||||
    android:width="24dp"
 | 
			
		||||
    android:height="24dp"
 | 
			
		||||
    android:viewportHeight="24"
 | 
			
		||||
    android:viewportWidth="24">
 | 
			
		||||
    <path
 | 
			
		||||
        android:fillColor="#ff000000"
 | 
			
		||||
        android:pathData="M6.62,10.79C8.06,13.62 10.38,15.94 13.21,17.38L15.41,15.18C15.69,14.9 16.08,14.82 16.43,14.93C17.55,15.3 18.75,15.5 20,15.5A1,1 0,0 1,21 16.5V20A1,1 0,0 1,20 21A17,17 0,0 1,3 4A1,1 0,0 1,4 3H7.5A1,1 0,0 1,8.5 4C8.5,5.25 8.7,6.45 9.07,7.57C9.18,7.92 9.1,8.31 8.82,8.59L6.62,10.79Z"/>
 | 
			
		||||
 | 
			
		||||
</vector>
 | 
			
		||||
@@ -12,10 +12,25 @@
 | 
			
		||||
		android:layout_height="wrap_content"
 | 
			
		||||
		android:text="AOHPCTCSeekBar"/>
 | 
			
		||||
 | 
			
		||||
	<LinearLayout
 | 
			
		||||
		android:orientation="vertical"
 | 
			
		||||
		android:layout_width="match_parent"
 | 
			
		||||
		android:layout_height="wrap_content"
 | 
			
		||||
		android:gravity="center">
 | 
			
		||||
 | 
			
		||||
		<cc.winboll.studio.libaes.views.AOHPCTCSeekBar
 | 
			
		||||
			android:layout_width="300dp"
 | 
			
		||||
			android:layout_height="wrap_content"
 | 
			
		||||
		android:id="@+id/fragmentviewpageAOHPCTCSeekBar1"/>
 | 
			
		||||
			android:id="@+id/fragmentviewpageAOHPCTCSeekBar1"
 | 
			
		||||
			android:padding="10dp"/>
 | 
			
		||||
 | 
			
		||||
		<cc.winboll.studio.libaes.views.AOHPCTCSeekBar
 | 
			
		||||
			android:layout_width="300dp"
 | 
			
		||||
			android:layout_height="wrap_content"
 | 
			
		||||
			android:id="@+id/fragmentviewpageAOHPCTCSeekBar2"
 | 
			
		||||
			android:padding="10dp"/>
 | 
			
		||||
 | 
			
		||||
	</LinearLayout>
 | 
			
		||||
 | 
			
		||||
	<androidx.viewpager.widget.ViewPager
 | 
			
		||||
		android:layout_width="match_parent"
 | 
			
		||||
@@ -56,5 +71,10 @@
 | 
			
		||||
 | 
			
		||||
	</LinearLayout>
 | 
			
		||||
 | 
			
		||||
	<cc.winboll.studio.libappbase.LogView
 | 
			
		||||
		android:layout_width="match_parent"
 | 
			
		||||
		android:layout_height="300dp"
 | 
			
		||||
		android:id="@+id/logview"/>
 | 
			
		||||
 | 
			
		||||
</LinearLayout>
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user