支持双向范围选择和普通模式的SeekBar

来源:互联网 发布:在线视频聊天源码 编辑:程序博客网 时间:2024/06/05 07:41

国际惯例先上图
这里写图片描述

作者的项目支持刻度和提示进度等。需要的可以戳
原作者Jay-Goo的项目GitHub地址

Step1

build.gradle增加

allprojects {    repositories {        ...        maven { url 'https://jitpack.io' }    }}dependencies {    ...    compile 'com.github.Jay-Goo:RangeSeekBar:v1.1.0'}//minSdkVersion的值改为16及以上minSdkVersion 16

Step2

xml增加

<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="com.xy.rangeseekbar.MainActivity"    android:orientation="vertical"    android:gravity="center"    android:background="#e5e5e5"    >    <TextView        android:id="@+id/progress2_tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:gravity="center"        android:background="@drawable/btn_dialog_box"        />    <com.jaygoo.widget.RangeSeekBar        android:id="@+id/seekbar2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        app:lineColorSelected="@color/colorPrimary"        app:lineColorEdge="@color/colorPrimaryDark"        app:thumbSize="20dp"        app:seekBarHeight="10dp"        app:cellMode="number"        app:seekBarMode="range"        app:progressHintMode="alwaysHide"        /></LinearLayout>

MainActivity.java中代码

public class MainActivity extends AppCompatActivity {    private RangeSeekBar seekbar2;    private TextView tv2;    private DecimalFormat df = new DecimalFormat("0");    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        seekbar2 = (RangeSeekBar)findViewById(R.id.seekbar2);        tv2 = (TextView)findViewById(R.id.progress2_tv);        //设置范围        seekbar2.setRange(0,179);        //设置初始值        seekbar2.setValue(50,100);        seekbar2.setOnRangeChangedListener(new RangeSeekBar.OnRangeChangedListener() {            @Override            public void onRangeChanged(RangeSeekBar view, float min, float max, boolean isFromUser) {                if (isFromUser) {                    tv2.setText(df.format(min) + "-" + df.format(max));                    seekbar2.setLeftProgressDescription(df.format(min));                    seekbar2.setRightProgressDescription(df.format(max));                }            }        });    }}

用于显示选择范围的是一个TextView,考虑到它可能会被拉伸,所以背景用的是一张.9图片,制作.9图片教程请戳这里

参数设置

attrformatdescriptionminfloat最小值, Float.MIN_VALUE <= min < max,默认:0maxfloat最大值, min < max <= Float.MAX_VALUE, 默认: 100reservefloat两个按钮的最小间距cellsintcells 等于0为普通模式,大于1时切换为刻度模式progressHintModeenum进度提示模式 defaultMode:当拖动时显示 alwaysHide:一直隐藏 alwaysShow:一直显示lineColorSelectedcolor拖动后的Seekbar颜色lineColorEdgecolor默认的Seekbar颜色thumbPrimaryColorcolor进度为最小值或最大值时按钮的颜色,默认:不调用thumbSecondaryColorcolor进度不为最小值或最大值时按钮的颜色,默认:不调用markTextArrayreference刻度文字,不设置的时候默认隐藏按钮的背景资源,不设置的时候默认为圆形按钮thumbResIdreference按钮的背景资源,不设置的时候默认为圆形按钮progressHintResIdreference进度提示背景资源,必须使用 9 path文件textPaddingdimension刻度文字与进度条之间的距离textSizehintBGHeightdimension进度提示背景的高度,不设置时根据文字尺寸自适应hintBGWithdimension进度提示背景的宽度,不设置时根据文字尺寸自适应hintBGPaddingdimension进度提示背景和进度条之间的距离seekBarHeightdimension进度条的高度thumbSizedimension按钮的尺寸cellModeenum刻度模式 number 根据刻度的实际所占比例分配位置*(markTextArray中必须都为数字)* other 平分当前布局*(markTextArray可以是任何字符)*seekBarModeenum单向、双向模式 single 单向模式,只有一个按钮 range 双向模式,有两个按钮

源码下载

本文Demo源码下载

原创粉丝点击