一. 简介

1
2
3
4
android.view.View
↳android.widget.ProgressBar
↳android.widget.AbsSeekBar
↳android.widget.SeekBar

SeekBar继承于ProgressBar,那么ProgressBar有的属性,我们SeekBar也有。SeekBar多了一个thumb属性,用来设置滑块的图标。

二. 属性

1
2
3
4
5
6
7
8
9
android:max=""              //最大进度值

android:progress="" //当前进度值

android:secondaryProgress=""//第二进度的值,常用来作为进度值的背景或者缓存进度

android:progressDrawable="" //progress的样式

android:thumb="" //滑块的图标

三. 自定义progressDrawable

1
2
//使用方式
android:progressDrawable="@drawable/progressbar_bg"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!--drawable文件夹下面的progressbar_bg-->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<!--设置背景颜色-->
<item android:id="@android:id/background"
android:drawable="@color/colorProgress"
/>

<!--设置缓冲条进度-->
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<!--圆角-->
<corners android:radius="50dp"/>

<!--进度条渐变颜色的初值和结束值-->
<gradient
android:endColor="#D2D2B0"
android:startColor="#D2D2B0"
/>
</shape>
</clip>
</item>

<!--设置进度条颜色-->
<item android:id="@android:id/progress">
<clip>
<shape>
<!--圆角-->
<corners android:radius="50dp"/>

<!--进度条渐变颜色的初值和结束值-->
<gradient
android:endColor="#fff000"
android:startColor="#fff000"
/>
</shape>
</clip>
</item>

</layer-list>

四. 监听滑动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
SeekBar seekBar = findViewById(R.id.seek_bar);

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//进度发生改变时会触发
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//按住SeekBar时会触发
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//放开SeekBar时触发
}
});

五. demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//active_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:orientation="vertical"
android:gravity="center">

<SeekBar
style="@style/Widget.AppCompat.SeekBar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="40dp"
android:indeterminate="false"
android:max="100"
android:progress="80"
android:secondaryProgress="100" />

<SeekBar
android:layout_width="match_parent"
android:layout_height="40dp"
android:progressDrawable="@drawable/seekbar_bg"
android:max="100"
android:progress="80"
android:secondaryProgress="100"
android:thumb="@drawable/seek_bar_point"
android:layout_marginTop="40dp"
/>

<SeekBar
android:layout_width="match_parent"
android:layout_height="40dp"
android:progressDrawable="@drawable/seekbar_bg"
android:max="100"
android:progress="80"
android:secondaryProgress="100"
android:thumb="@drawable/seek_bar_point"
android:background="@null"
android:layout_marginTop="40dp"
/>

<SeekBar
android:id="@+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:progressDrawable="@drawable/seekbar_bg"
android:max="100"
android:progress="80"
android:secondaryProgress="100"
android:thumb="@drawable/seek_bar_point"
android:splitTrack="false"
android:background="@null"
android:layout_marginTop="40dp"
/>

<TextView
android:id="@+id/show_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="80"
android:textAlignment="center"
android:textStyle="bold"
android:layout_marginTop="40dp"
/>

</LinearLayout>

//MainActivity
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final TextView textView = findViewById(R.id.show_text);

SeekBar seekBar = findViewById(R.id.seek_bar);

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
textView.setText(String.valueOf(progress));
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});
}
}

运行效果:

参考文章

Android之ProgressBar(进度条)和SeekBar(拖动条)

seekbar thumb 透明效果出现父布局背景颜色的解决方法

解决SeekBar拖动过程中thumb周围产生的圆形阴影/白色圆圈