简体   繁体   English

如何设置自定义滑块的值

[英]How to set the value of custom slider

I am creating a slider control, I have problems setting the value though 我正在创建一个滑块控件,但是在设置值时遇到了问题

_x = 512;
_y = 256;
_width = 128;
_height = 12;
_min = 0;
_max = 10;

I do something like, 我做类似的事情,

// set the value
_input = 5;
_value = median(_x-(_width/2)+(_input/_max),_x-((_width/2)),_x+((_width/2)))

Setting the _input to 5 should put the slider in the middle, but it doesn't, I think I have the calculation wrong in some way. 将_input设置为5应该将滑块放在中间,但事实并非如此,我认为我在某种程度上计算有误。

_x-(_width/2) will give you 0 in percent, 0 in value _x-(_ width / 2)将为您提供0的百分比,0的值

_x+(_width/2) will give you 100 in percent, 10 in value. _x +(_ width / 2)将为您提供100%的价值,10个价值。 The max. 最高

Mapping x0 € [_x-_width/2 .. _x+_width/2] to sl € [0..10] linearly and vica versa is not a big deal. x0 € [_x-_width/2 .. _x+_width/2] sl € [0..10]线性映射到sl € [0..10] ,反之亦然。

If having x0 : offset of -(_x-_width/2) transforms it to 0.._width , so a scale of 10/_width is needed for 0..10 : 如果x0-(_x-_width/2)偏移量将其转换为0.._width ,那么0..10的比例尺需要为10/_width

sl == ( x0 - (_x-_width/2) ) * 10 / _width    // float div unless 10 | _width

Reversed: 相反:

x0 == (_x-_width/2) + sl * _width / 10

Take care of int vs. float divisions and rounding ( +.5 ). 注意intfloat除法和舍入( +.5 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM