Android中自定义Seekbar分段的方法

Android中自定义Seekbar分段的方法

在这里加入将一个Seekbar,平均分段为8段,每次拖动的距离相等

代码如下

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
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
Log.i(TAG, "----onStopTrackingTouch----");
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
Log.i(TAG, "----onStartTrackingTouch----");
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (fromUser) {
float pro = seekBar.getProgress();
float max = seekBar.getMax();
float result = (pro / max) * 100;
if (result == 0) {
seekBar.setProgress(0);
} else if (result > 0 && result <= 12.5) {
seekBar.setProgress(25 / 2);
} else if (result > 12.5 && result <= 25) {
seekBar.setProgress(25);
} else if (result > 25 && result <= 37.5) {
seekBar.setProgress(25 + (25 / 2));
} else if (result > 37.5 && result <= 50) {
seekBar.setProgress(50);
} else if (result > 50 && result <= 62.5) {
seekBar.setProgress(50 + (25 / 2));
} else if (result > 62.5 && result <= 75) {
seekBar.setProgress(75);
} else if (result > 75 && result <= 87.5) {
seekBar.setProgress(75 + (25 / 2));
} else if (result > 87.5 && result <= 100) {
seekBar.setProgress(100);
}
}
}
});
0%