Deh*_*zer -4 java math android
我想获得介于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);
Run Code Online (Sandbox Code Playgroud)
第二个(工作,但不是很好):
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;
}
Run Code Online (Sandbox Code Playgroud)
根据你的描述,我猜你想要的公式是这样的:
result = -0.5 + 0.5*( (in - min) / (max - min) );
Run Code Online (Sandbox Code Playgroud)
但既然你没有展示任何代码也没有解释它的目的,那只是一个疯狂的猜测.