Android自定义双向滑动控件

本文实例为大家分享了Android自定义双向滑动控件的具体代码,供大家参考,具体内容如下

Posted in Java/Android onApril 19, 2022

先看一下效果图

Android自定义双向滑动控件

1.SeekBarPressure工具类

public class SeekBarPressure extends View {
    private static final String TAG = "SeekBarPressure";
    private static final int CLICK_ON_LOW = 1;      //点击在前滑块上
    private static final int CLICK_ON_HIGH = 2;     //点击在后滑块上
    private static final int CLICK_IN_LOW_AREA = 3;
    private static final int CLICK_IN_HIGH_AREA = 4;
    private static final int CLICK_OUT_AREA = 5;
    private static final int CLICK_INVAILD = 0;
    /*
     * private static final int[] PRESSED_STATE_SET = {
     * android.R.attr.state_focused, android.R.attr.state_pressed,
     * android.R.attr.state_selected, android.R.attr.state_window_focused, };
     */
    private static final int[] STATE_NORMAL = {};
    private static final int[] STATE_PRESSED = {
            android.R.attr.state_pressed, android.R.attr.state_window_focused,
    };
    private Drawable hasScrollBarBg;        //滑动条滑动后背景图
    private Drawable notScrollBarBg;        //滑动条未滑动背景图
    private Drawable mThumbLow;         //前滑块
    private Drawable mThumbHigh;        //后滑块
 
    private int mScollBarWidth;     //控件宽度=滑动条宽度+滑动块宽度
    private int mScollBarHeight;    //滑动条高度
 
    private int mThumbWidth;        //滑动块宽度
    private int mThumbHeight;       //滑动块高度
 
    private double mOffsetLow = 0;     //前滑块中心坐标
    private double mOffsetHigh = 0;    //后滑块中心坐标
    private int mDistance = 0;      //总刻度是固定距离 两边各去掉半个滑块距离
 
    private int mThumbMarginTop = 30;   //滑动块顶部距离上边框距离,也就是距离字体顶部的距离
 
    private int mFlag = CLICK_INVAILD;
    private OnSeekBarChangeListener mBarChangeListener;
 
 
    private double defaultScreenLow = 0;    //默认前滑块位置百分比
    private double defaultScreenHigh = 100;  //默认后滑块位置百分比
 
    private boolean isEdit = false;     //输入框是否正在输入
 
    public SeekBarPressure(Context context) {
        this(context, null);
    }
 
    public SeekBarPressure(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public SeekBarPressure(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
//        this.setBackgroundColor(Color.BLACK);
 
        Resources resources = getResources();
        notScrollBarBg = resources.getDrawable(R.color.red);
        hasScrollBarBg = resources.getDrawable(R.color.blue);
        mThumbLow = resources.getDrawable(R.drawable.blue);
        mThumbHigh = resources.getDrawable(R.drawable.red);
 
        mThumbLow.setState(STATE_NORMAL);
        mThumbHigh.setState(STATE_NORMAL);
 
        mScollBarWidth = notScrollBarBg.getIntrinsicWidth();
        mScollBarHeight = notScrollBarBg.getIntrinsicHeight();
 
        mThumbWidth = mThumbLow.getIntrinsicWidth();
        mThumbHeight = mThumbLow.getIntrinsicHeight();
 
    }
 
    //默认执行,计算view的宽高,在onDraw()之前
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = measureWidth(widthMeasureSpec);
//        int height = measureHeight(heightMeasureSpec);
        mScollBarWidth = width;
        mOffsetHigh = width - mThumbWidth / 2;
        mOffsetLow = mThumbWidth / 2;
        mDistance = width - mThumbWidth;
 
        mOffsetLow = formatDouble(defaultScreenLow / 100 * (mDistance ))+ mThumbWidth / 2;
        mOffsetHigh = formatDouble(defaultScreenHigh / 100 * (mDistance)) + mThumbWidth / 2;
        setMeasuredDimension(width, mThumbHeight + mThumbMarginTop+2);
    }
 
 
    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        //wrap_content
        if (specMode == MeasureSpec.AT_MOST) {
        }
        //fill_parent或者精确值
        else if (specMode == MeasureSpec.EXACTLY) {
        }
 
        return specSize;
    }
 
    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int defaultHeight = 100;
        //wrap_content
        if (specMode == MeasureSpec.AT_MOST) {
        }
        //fill_parent或者精确值
        else if (specMode == MeasureSpec.EXACTLY) {
            defaultHeight = specSize;
        }
 
        return defaultHeight;
    }
 
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }
 
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
 
//        Paint text_Paint = new Paint();
//        text_Paint.setTextAlign(Paint.Align.CENTER);
//        text_Paint.setColor(Color.RED);
//        text_Paint.setTextSize(50);
 
        int aaa = mThumbMarginTop + mThumbHeight / 2 - mScollBarHeight / 2;
        int bbb = aaa + mScollBarHeight;
 
        //白色,不会动
        notScrollBarBg.setBounds(mThumbWidth / 2, aaa, mScollBarWidth - mThumbWidth / 2, bbb);
        notScrollBarBg.draw(canvas);
 
        //蓝色,中间部分会动
        hasScrollBarBg.setBounds((int)mOffsetLow, aaa, (int)mOffsetHigh, bbb);
        hasScrollBarBg.draw(canvas);
 
        //前滑块
        mThumbLow.setBounds((int)(mOffsetLow - mThumbWidth / 2), mThumbMarginTop, (int)(mOffsetLow + mThumbWidth / 2), mThumbHeight + mThumbMarginTop);
        mThumbLow.draw(canvas);
 
        //后滑块
        mThumbHigh.setBounds((int)(mOffsetHigh - mThumbWidth / 2), mThumbMarginTop, (int)(mOffsetHigh + mThumbWidth / 2), mThumbHeight + mThumbMarginTop);
        mThumbHigh.draw(canvas);
 
        double progressLow = formatDouble((mOffsetLow - mThumbWidth / 2) * 100 / mDistance);
        double progressHigh = formatDouble((mOffsetHigh - mThumbWidth / 2) * 100 / mDistance);
//            Log.d(TAG, "onDraw-->mOffsetLow: " + mOffsetLow + "  mOffsetHigh: " + mOffsetHigh   + "  progressLow: " + progressLow + "  progressHigh: " + progressHigh);
       // canvas.drawText((int) progressLow + "", (int)mOffsetLow - 2 - 2, 15, text_Paint);
       // canvas.drawText((int) progressHigh + "", (int)mOffsetHigh - 2, 15, text_Paint);
 
        if (mBarChangeListener != null) {
            if (!isEdit) {
                mBarChangeListener.onProgressChanged(this, progressLow, progressHigh);
            }
 
        }
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        //按下
        if (e.getAction() == MotionEvent.ACTION_DOWN) {
            if (mBarChangeListener != null) {
                mBarChangeListener.onProgressBefore();
                isEdit = false;
            }
            mFlag = getAreaFlag(e);
//            Log.d(TAG, "e.getX: " + e.getX() + "mFlag: " + mFlag);
//            Log.d("ACTION_DOWN", "------------------");
            if (mFlag == CLICK_ON_LOW) {
                mThumbLow.setState(STATE_PRESSED);
            } else if (mFlag == CLICK_ON_HIGH) {
                mThumbHigh.setState(STATE_PRESSED);
            } else if (mFlag == CLICK_IN_LOW_AREA) {
                mThumbLow.setState(STATE_PRESSED);
                //如果点击0-mThumbWidth/2坐标
                if (e.getX() < 0 || e.getX() <= mThumbWidth/2) {
                    mOffsetLow = mThumbWidth/2;
                } else if (e.getX() > mScollBarWidth - mThumbWidth/2) {
//                    mOffsetLow = mDistance - mDuration;
                    mOffsetLow = mThumbWidth/2 + mDistance;
                } else {
                    mOffsetLow = formatDouble(e.getX());
//                    if (mOffsetHigh<= mOffsetLow) {
//                        mOffsetHigh = (mOffsetLow + mDuration <= mDistance) ? (mOffsetLow + mDuration)
//                                : mDistance;
//                        mOffsetLow = mOffsetHigh - mDuration;
//                    }
                }
            } else if (mFlag == CLICK_IN_HIGH_AREA) {
                mThumbHigh.setState(STATE_PRESSED);
//                if (e.getX() < mDuration) {
//                    mOffsetHigh = mDuration;
//                    mOffsetLow = mOffsetHigh - mDuration;
//                } else if (e.getX() >= mScollBarWidth - mThumbWidth/2) {
//                    mOffsetHigh = mDistance + mThumbWidth/2;
                if(e.getX() >= mScollBarWidth - mThumbWidth/2) {
                    mOffsetHigh = mDistance + mThumbWidth/2;
                } else {
                    mOffsetHigh = formatDouble(e.getX());
//                    if (mOffsetHigh <= mOffsetLow) {
//                        mOffsetLow = (mOffsetHigh - mDuration >= 0) ? (mOffsetHigh - mDuration) : 0;
//                        mOffsetHigh = mOffsetLow + mDuration;
//                    }
                }
            }
            //设置进度条
            refresh();
 
            //移动move
        } else if (e.getAction() == MotionEvent.ACTION_MOVE) {
//            Log.d("ACTION_MOVE", "------------------");
            if (mFlag == CLICK_ON_LOW) {
                if (e.getX() < 0 || e.getX() <= mThumbWidth/2) {
                    mOffsetLow = mThumbWidth/2;
                } else if (e.getX() >= mScollBarWidth - mThumbWidth/2) {
                    mOffsetLow = mThumbWidth/2 + mDistance;
                    mOffsetHigh = mOffsetLow;
                } else {
                    mOffsetLow = formatDouble(e.getX());
                    if (mOffsetHigh - mOffsetLow <= 0) {
                        mOffsetHigh = (mOffsetLow <= mDistance+mThumbWidth/2) ? (mOffsetLow) : (mDistance+mThumbWidth/2);
                    }
                }
            } else if (mFlag == CLICK_ON_HIGH) {
                if (e.getX() <  mThumbWidth/2) {
                    mOffsetHigh = mThumbWidth/2;
                    mOffsetLow = mThumbWidth/2;
                } else if (e.getX() > mScollBarWidth - mThumbWidth/2) {
                    mOffsetHigh = mThumbWidth/2 + mDistance;
                } else {
                    mOffsetHigh = formatDouble(e.getX());
                    if (mOffsetHigh - mOffsetLow <= 0) {
                        mOffsetLow = (mOffsetHigh >= mThumbWidth/2) ? (mOffsetHigh) : mThumbWidth/2;
                    }
                }
            }
            //设置进度条
            refresh();
            //抬起
        } else if (e.getAction() == MotionEvent.ACTION_UP) {
//            Log.d("ACTION_UP", "------------------");
            mThumbLow.setState(STATE_NORMAL);
            mThumbHigh.setState(STATE_NORMAL);
 
            if (mBarChangeListener != null) {
                mBarChangeListener.onProgressAfter();
            }
            //这两个for循环 是用来自动对齐刻度的,注释后,就可以自由滑动到任意位置
//            for (int i = 0; i < money.length; i++) {
//                 if(Math.abs(mOffsetLow-i* ((mScollBarWidth-mThumbWidth)/ (money.length-1)))<=(mScollBarWidth-mThumbWidth)/(money.length-1)/2){
//                     mprogressLow=i;
//                     mOffsetLow =i* ((mScollBarWidth-mThumbWidth)/(money.length-1));
//                     invalidate();
//                     break;
//                }
//            }
//
//            for (int i = 0; i < money.length; i++) {
//                  if(Math.abs(mOffsetHigh-i* ((mScollBarWidth-mThumbWidth)/(money.length-1) ))<(mScollBarWidth-mThumbWidth)/(money.length-1)/2){
//                      mprogressHigh=i;
//                       mOffsetHigh =i* ((mScollBarWidth-mThumbWidth)/(money.length-1));
//                       invalidate();
//                       break;
//                }
//            }
        }
        return true;
    }
 
    public int getAreaFlag(MotionEvent e) {
 
        int top = mThumbMarginTop;
        int bottom = mThumbHeight + mThumbMarginTop;
        if (e.getY() >= top && e.getY() <= bottom && e.getX() >= (mOffsetLow - mThumbWidth / 2) && e.getX() <= mOffsetLow + mThumbWidth / 2) {
            return CLICK_ON_LOW;
        } else if (e.getY() >= top && e.getY() <= bottom && e.getX() >= (mOffsetHigh - mThumbWidth / 2) && e.getX() <= (mOffsetHigh + mThumbWidth / 2)) {
            return CLICK_ON_HIGH;
        } else if (e.getY() >= top
                && e.getY() <= bottom
                && ((e.getX() >= 0 && e.getX() < (mOffsetLow - mThumbWidth / 2)) || ((e.getX() > (mOffsetLow + mThumbWidth / 2))
                && e.getX() <= ((double) mOffsetHigh + mOffsetLow) / 2))) {
            return CLICK_IN_LOW_AREA;
        } else if (e.getY() >= top
                && e.getY() <= bottom
                && (((e.getX() > ((double) mOffsetHigh + mOffsetLow) / 2) && e.getX() < (mOffsetHigh - mThumbWidth / 2)) || (e
                .getX() > (mOffsetHigh + mThumbWidth/2) && e.getX() <= mScollBarWidth))) {
            return CLICK_IN_HIGH_AREA;
        } else if (!(e.getX() >= 0 && e.getX() <= mScollBarWidth && e.getY() >= top && e.getY() <= bottom)) {
            return CLICK_OUT_AREA;
        } else {
            return CLICK_INVAILD;
        }
    }
 
    //更新滑块
    private void refresh() {
        invalidate();
    }
 
    //设置前滑块的值
    public void setProgressLow(double  progressLow) {
        this.defaultScreenLow = progressLow;
        mOffsetLow = formatDouble(progressLow / 100 * (mDistance ))+ mThumbWidth / 2;
        isEdit = true;
        refresh();
    }
 
    //设置后滑块的值
    public void setProgressHigh(double  progressHigh) {
        this.defaultScreenHigh = progressHigh;
        mOffsetHigh = formatDouble(progressHigh / 100 * (mDistance)) + mThumbWidth / 2;
        isEdit = true;
        refresh();
    }
 
    public void setOnSeekBarChangeListener(OnSeekBarChangeListener mListener) {
        this.mBarChangeListener = mListener;
    }
 
    //回调函数,在滑动时实时调用,改变输入框的值
    public interface OnSeekBarChangeListener {
        //滑动前
        public void onProgressBefore();
 
        //滑动时
        public void onProgressChanged(SeekBarPressure seekBar, double progressLow,
                                      double progressHigh);
 
        //滑动后
        public void onProgressAfter();
    }
 
/*    private int formatInt(double value) {
        BigDecimal bd = new BigDecimal(value);
        BigDecimal bd1 = bd.setScale(0, BigDecimal.ROUND_HALF_UP);
        return bd1.intValue();
    }*/
 
    public static double formatDouble(double pDouble) {
        BigDecimal bd = new BigDecimal(pDouble);
        BigDecimal bd1 = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
        pDouble = bd1.doubleValue();
        return pDouble;
    }
 
}

2.activity_main.xml 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#000"
    android:orientation="vertical">
 
    <com.yjjk.doubleseekbarsss.SeekBarPressure
        android:id="@+id/seekBar_tg2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" />
    <TextView
        android:id="@+id/editTexts_min"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textColor="#fff"></TextView>
    <TextView
        android:id="@+id/editTexts_max"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textColor="#fff"></TextView>
 
 
</LinearLayout>

3.MainActivity

public class MainActivity extends AppCompatActivity {
 
    private boolean isScreen;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //id
        final SeekBarPressure seekBarPressures = findViewById(R.id.seekBar_tg2);
        final TextView editTexts_min = findViewById(R.id.editTexts_min);
        final TextView editTexts_max = findViewById(R.id.editTexts_max);
 
        seekBarPressures.setOnSeekBarChangeListener(new SeekBarPressure.OnSeekBarChangeListener() {
            @Override
            public void onProgressBefore() {
                isScreen = true;
            }
 
            @Override
            public void onProgressChanged(SeekBarPressure seekBar, double progressLow, double progressHigh) {
 
                editTexts_min.setText((int) progressLow + "");
                editTexts_max.setText((int) progressHigh + "");
 
                //获取SharedPreferences对象
                SharedPreferences sharedPre=getSharedPreferences("config", MODE_PRIVATE);
 
                //获取Editor对象
                SharedPreferences.Editor edit = sharedPre.edit();
                edit.clear();
 
                //设置参数
                edit.putString("progressHigh", String.valueOf(progressHigh));
                edit.putString("progressLow", String.valueOf(progressLow));
 
                //提交
                edit.commit();
 
            }
 
            @Override
            public void onProgressAfter() {
                isScreen = false;
            }
        });
 
 
        //取值
        SharedPreferences sharedPre=getSharedPreferences("config",MODE_PRIVATE);
 
        String progressHighs = sharedPre.getString("progressHigh", "");
        String progressLows = sharedPre.getString("progressLow", "");
 
        if (progressHighs.length()!=0){
            seekBarPressures.setProgressHigh(Double.parseDouble(progressHighs));
            seekBarPressures.setProgressLow(Double.parseDouble(progressLows));
 
            editTexts_max.setText(progressHighs);
            editTexts_min.setText(progressLows);
        }else {
            seekBarPressures.setProgressLow(30);
            seekBarPressures.setProgressHigh(70);
 
            editTexts_min.setText("最小值:"+30);
            editTexts_max.setText("最大值:"+70);
        }
    }
}

以上就是本文的全部内容。

Java/Android 相关文章推荐
浅谈Java实现分布式事务的三种方案
Jun 11 Java/Android
Java数据结构之链表相关知识总结
Jun 18 Java/Android
java设计模式--建造者模式详解
Jul 21 Java/Android
SpringRetry重试框架的具体使用
Jul 25 Java/Android
SpringBoot整合RabbitMQ的5种模式实战
Aug 02 Java/Android
IDEA2021.2配置docker如何将springboot项目打成镜像一键发布部署
Sep 25 Java/Android
Java 实现限流器处理Rest接口请求详解流程
Nov 02 Java/Android
springboot中的pom文件 project报错问题
Jan 18 Java/Android
Spring Boot项目传参校验的最佳实践指南
Apr 05 Java/Android
Java8 Stream API 提供了一种高效且易于使用的处理数据的方式
Apr 13 Java/Android
Java 多态分析
Apr 26 Java/Android
springboot集成redis存对象乱码的问题及解决
Jun 16 Java/Android
java高级用法JNA强大的Memory和Pointer
Apr 19 #Java/Android
Java后端 Dubbo retries 超时重试机制的解决方案
Apr 14 #Java/Android
Java数组详细介绍及相关工具类
Apr 14 #Java/Android
Java8利用Stream对列表进行去除重复的方法详解
Apr 14 #Java/Android
详解Flutter网络请求Dio库的使用及封装
Apr 14 #Java/Android
详细介绍Java中的CyclicBarrier
Apr 13 #Java/Android
Java8 Stream API 提供了一种高效且易于使用的处理数据的方式
Apr 13 #Java/Android
You might like
php5 apache 2.2 webservice 创建与配置(java)
2011/01/27 PHP
Codeigniter实现智能裁剪图片的方法
2014/06/12 PHP
php中使用gd库实现下载网页中所有图片
2015/05/12 PHP
php传值赋值和传地址赋值用法实例分析
2015/06/20 PHP
Yii框架扩展CGridView增加导出CSV功能的方法
2017/05/24 PHP
基于jquery用于查询操作的实现代码
2010/05/10 Javascript
jQuery随手笔记之常用的jQuery操作DOM事件
2015/11/29 Javascript
JavaScript截取、切割字符串的技巧
2016/01/07 Javascript
jQuery设置Cookie及删除Cookie实例分析
2016/04/15 Javascript
禁用backspace网页回退功能的实现代码
2016/11/15 Javascript
Vue数据驱动模拟实现3
2017/01/11 Javascript
js时间戳格式化成日期格式的多种方法介绍
2017/02/16 Javascript
nodejs处理图片的中间件node-images详解
2017/05/08 NodeJs
Vue服务器渲染Nuxt学习笔记
2018/01/31 Javascript
手淘flexible.js框架使用和源代码讲解小结
2018/10/15 Javascript
Vue监听页面刷新和关闭功能
2019/06/20 Javascript
[53:15]2018DOTA2亚洲邀请赛3月29日 小组赛A组 KG VS OG
2018/03/30 DOTA
[08:08]DOTA2-DPC中国联赛2月28日Recap集锦
2021/03/11 DOTA
在python中的socket模块使用代理实例
2014/05/29 Python
python列表操作实例
2015/01/14 Python
python中virtualenvwrapper安装与使用
2018/05/20 Python
python爬虫获取小区经纬度以及结构化地址
2018/12/30 Python
基于python解线性矩阵方程(numpy中的matrix类)
2019/10/21 Python
Django框架模板用法入门教程
2019/11/04 Python
在python3.64中安装pyinstaller库的方法步骤
2020/06/02 Python
CSS3 animation ? steps 函数详解
2019/08/30 HTML / CSS
精灵市场:Pixie Market
2019/06/18 全球购物
应届毕业生的自我鉴定
2013/11/13 职场文书
如何客观的进行自我评价
2013/12/17 职场文书
函授毕业自我鉴定
2013/12/19 职场文书
大学自我鉴定范文
2013/12/26 职场文书
先进个人获奖感言
2014/01/24 职场文书
群众路线剖析材料(四风)
2014/11/05 职场文书
转正申请报告格式
2015/05/15 职场文书
2016反腐倡廉警示教育心得体会
2016/01/13 职场文书
Python深度学习之Pytorch初步使用
2021/05/20 Python