Why is this so hard to find out?
public boolean onTouch(View v, MotionEvent event) I need to convert float event.getY() to an int.
Is this possible?
event.getY().intValue() will not work at all.
Any ideas?
Uhhh, yeah, how about:
int y = (int)event.getY(); You see getY() only returns a float for devices that have a sub-pixel accuracy.
Using
Math.round(yourFloat);
is better than
(int)yourFloat;
It is all about precision. If you use (int) you'll just get numbers after the point removed. If you use Math, you'll get a rounded number. It doesn't seems like a big deal.For example, if you try to round something like 3.1, both methods would produce the same result - 3.
But take 3.9 or 3.8. It's practically 4, yet
(int)3.9 = 3
whereas
Math.round(3.9) = 4
some_float. If it has 1 decimal after point, like x.5 to x.9, then result will be x + 1 (x.5 + 0.5 = x+1), if x.0 to x.04 - just x (x.4 + 0.5 = x.9). But still, this won't work as Math.round. 1.46 + 0.5 = 1.96. (int)1.96 = 1. In that case, to get precise result, you would need to have some_float + 0.5555555555...infinityJust cast it:
int val = (int)event.getY();