将进度/搜索栏比例更改为非线性(例如对数)

时间:2012-11-25 17:33:20

标签: android progress-bar seekbar

是否可以将Progress-或Seekbars的缩放比例更改为非线性?

如果是这样,怎么改变?

1 个答案:

答案 0 :(得分:4)

例如:

weightSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

// place onStartTrackingTouch(SeekBar seekBar) and onStopTrackingTouch(SeekBar seekBar) here

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        userWeight = ((progress * progress) / (1000.0f * 1000.0f)) * (maxWeight - minWeight) + minWeight; // Approximate an exponential curve with x^2.
        }
    });

反之亦然:

weightSeekBar.setProgress((int) (Math.sqrt(((userWeight - minWeight) / (maxWeight - minWeight)) * 1000.0f * 1000.0f)));