我想获得介于 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;
}