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

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

做者的项目支持刻度和提示进度等。须要的能够戳
原做者Jay-Goo的项目GitHub地址android

Step1git

build.gradle增长github

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    ...
    compile 'com.github.Jay-Goo:RangeSeekBar:v1.1.0'
}

//minSdkVersion的值改成16及以上
minSdkVersion 16

Step2web

xml增长app

<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中代码maven

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图片教程请戳这里ide

参数设置

attr format description
min float 最小值, Float.MIN_VALUE <= min < max,默认:0
max float 最大值, min < max <= Float.MAX_VALUE, 默认: 100
reserve float 两个按钮的最小间距
cells int cells 等于0为普通模式,大于1时切换为刻度模式
progressHintMode enum 进度提示模式 defaultMode:当拖动时显示 alwaysHide:一直隐藏 alwaysShow:一直显示
lineColorSelected color 拖动后的Seekbar颜色
lineColorEdge color 默认的Seekbar颜色
thumbPrimaryColor color 进度为最小值或最大值时按钮的颜色,默认:不调用
thumbSecondaryColor color 进度不为最小值或最大值时按钮的颜色,默认:不调用
markTextArray reference 刻度文字,不设置的时候默认隐藏按钮的背景资源,不设置的时候默认为圆形按钮
thumbResId reference 按钮的背景资源,不设置的时候默认为圆形按钮
progressHintResId reference 进度提示背景资源,必须使用 9 path文件
textPadding dimension 刻度文字与进度条之间的距离textSize
hintBGHeight dimension 进度提示背景的高度,不设置时根据文字尺寸自适应
hintBGWith dimension 进度提示背景的宽度,不设置时根据文字尺寸自适应
hintBGPadding dimension 进度提示背景和进度条之间的距离
seekBarHeight dimension 进度条的高度
thumbSize dimension 按钮的尺寸
cellMode enum 刻度模式 number 根据刻度的实际所占比例分配位置*(markTextArray中必须都为数字)* other 平分当前布局*(markTextArray能够是任何字符)*
seekBarMode enum 单向、双向模式 single 单向模式,只有一个按钮 range 双向模式,有两个按钮

源码下载

本文Demo源码下载svg