Android SeekBar Minimum and Continuous Float Value Attributes
I'm looking for a way to implement a minimum value in my SeekBar and also have the option to increment decimal numbers. For example, currently my SeekBar's 开发者_开发技巧minimum is set to 0, but I need it to start at the value 0.2. Also, I would like to have the functionality to be able to have the user select a number from 0.2 to 10.0 at a .1 precision so they can choose the numbers 5.6 or 7.1.
Here are the style attributes for my SeekBar:
<style name="SeekBar">
<item name="android:paddingLeft">30dp</item>
<item name="android:paddingRight">30dp</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginTop">0dp</item>
<item name="android:layout_marginBottom">0dp</item>
<item name="android:max">10</item>
<item name="android:progress">1</item>
</style>
The values that can be used for the progress are limited to values between zero and some max value that we choose. We can however scale these values with arithmetic operations. Lets for example set the max value to 200 in the XML-file. Then change the java code in the following way.
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
sliderValueTW.setText(Float.toString((float)(progress-100)/10));
}
The result is that the slider appears to the user to be going from -10.0 to 10.0 with increments of 0.1 units.
to set the starting progress you can do:
SeekBar yourSeekBar = (SeekBar) findViewById(R.id.seek_bar);
yourSeekBar.setProgress(20);
That should show your seekbar with your desired starting position (at 0.2) if the max is set to 100.
I see you already have the first part which is how to do floating point values. Here's how I implement a minimum constraint: (ignoring the floating point value conversion)
int mySbValue;
SeekBar sb = (SeekBar) findViewById(R.id.seekbar);
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress < MIN) {
mySbValue = MIN;
} else {
mySbValue = progress;
}
}
...
Copy this class and use custom Seek Bar :
public class FloatSeekBar extends SeekBar implements SeekBar.OnSeekBarChangeListener {
private OnFloatSeekBarChangeListener floatListener;
private float floatMaxValue = 100.f;
private float floatPrgress = 0.f;
private float minPrgress = 0.f;
public float getMinPrgress() {
return minPrgress;
}
public void setMin(float minPrgress) {
this.minPrgress = minPrgress;
int middle = getMiddle(floatMaxValue, minPrgress);
super.setMax(middle);
}
private int getMiddle(float floatMaxValue, float minPrgress) {
float v = floatMaxValue - minPrgress;
int round = Math.round(v * 10);
return round;
}
public float getFloatPrgress() {
return floatPrgress;
}
public void setFloatPrgress(float floatPrgress) {
this.floatPrgress = floatPrgress;
int i = Math.round((floatPrgress - minPrgress) * 10);
super.setProgress(i);
}
public FloatSeekBar(Context context) {
super(context);
this.setOnSeekBarChangeListener(this);
}
public FloatSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.FloatSeekBar, 0, 0);
floatMaxValue = typedArray.getFloat(R.styleable.FloatSeekBar_maxValue, 100f);
minPrgress = typedArray.getFloat(R.styleable.FloatSeekBar_minValue, 0f);
floatPrgress = typedArray.getFloat(R.styleable.FloatSeekBar_floatProgress, 0f);
setFloatPrgress(floatPrgress);
this.setOnSeekBarChangeListener(this);
}
public float getMaxProgress() {
return floatMaxValue;
}
public void setMax(float value) {
floatMaxValue = value;
int middle = getMiddle(floatMaxValue, minPrgress);
super.setMax(middle);
}
public void setOnFloatSeekBarChangeListener(OnFloatSeekBarChangeListener floatListener) {
this.floatListener = floatListener;
int middle = getMiddle(floatMaxValue, minPrgress);
super.setMax(middle);
}
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
floatPrgress = minPrgress + i / 10.0f;
floatListener.onFloatSeekProgressChanged(seekBar, floatPrgress, b);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
floatListener.onStartTrackingTouch(seekBar);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
floatListener.onStopTrackingTouch(seekBar);
}
public static interface OnFloatSeekBarChangeListener {
public void onFloatSeekProgressChanged(SeekBar seekBar, float i, boolean b);
public void onStartTrackingTouch(SeekBar seekBar);
public void onStopTrackingTouch(SeekBar seekBar);
}
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}
精彩评论