-4

我想获得介于 0.0 和 -0.5 之间的结果。我有值:MIN = x,MAX = y 和 IN = x。值 MIN 应产生 -0.5% 和 MAX 0.0% 的百分比。例如,如果 MIN 的值为 240px,MAX 为 600px,IN 为 360px,则 IN 的百分比应为 -0.33%。但我不知道如何进行这个计算。

PS:IN不能高于0.0或低于-0.5。PS2:对不起我的英语。

我试过的代码,但没有用:

float percent = (((currentX / max) * min) / (max - min) * (-1)); Animation openNavigationDrawer = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, percent, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); openNavigationDrawer.setDuration(200); navigationDrawer.setAnimation(openNavigationDrawer);

第二个(有效,但不好):

float percent = -0.0f;
    float posDividerDefault = max / 12, 
    posDividerOne = min + posDividerDefault, posDividerTwo = posDividerOne + posDividerDefault, 
    posDividerThree = posDividerTwo + posDividerDefault, posDividerFour = posDividerThree + posDividerDefault, 
    posDividerFive = posDividerFour + posDividerDefault, posDividerSix = posDividerFive + posDividerDefault;

    if (currentX < posDividerOne) {
        percent = -0.5f;

    } else if (currentX > posDividerOne && currentX < posDividerTwo) {
        percent = -0.45f;

    } else if (currentX > posDividerTwo && currentX < posDividerThree) {
        percent = -0.4f;

    } else if (currentX > posDividerThree && currentX < posDividerFour) {
        percent = -0.3f;

    } else if (currentX > posDividerFour && currentX < posDividerFive) {
        percent = -0.2f;

    } else if (currentX > posDividerFive && currentX < posDividerSix) {
        percent = -0.1f;

    } else if (currentX > posDividerSix) {
        percent = -0.0f;

    }
4

2 回答 2

5

根据您的描述,我猜您想要的公式是这样的:

result = -0.5 + 0.5*( (in - min) / (max - min) );

但是由于您没有显示任何代码,也没有解释它的目的,所以这只是一个疯狂的猜测。

于 2013-08-15T13:33:59.093 回答
0

与@MightyPork 完全相同,但恕我直言,更清楚地展示了正在发生的事情:

static final double MIN = -0.5;
static final double MAX = 0.0;
public void test(double x, double min, double max) {
  // (x - min)     = translate to min
  // / (max - min) = scale to unit
  // * (MAX - MIN) = scale to final
  // + MIN         = translate to MIN
  double v = (x - min) / (max - min) * (MAX - MIN) + MIN;
  System.out.println("test("+x+","+min+","+max+") = "+v);
}
于 2013-08-15T13:58:12.820 回答