I faced a kind of problem. The problem is that MotionEvent#getY() doesn't returns expected values, while MotionEvent#getRawY() keeps returning expected, sequential positions. So I tried to use MotionEvent#getRawY() instead of MotionEvent#getY(). However, values getRawY() sent are based on the absolute position of displays. Some sites show hacks to exclude unnecessary height of, such as the status bar. But the heights of the bars are not same on all platforms and sometimes the bars are even got hidden.
From Android API Level 1(This is not my mistake, indeed from level ONE!), the View class provide getLocationOnScreen(int[]) method. Using this method, you can convert the result of MotionEvent#getRawY() into relative to your views.
class MyView implements OnTouchListener
{
@Override
public boolean onTouch(View pmView, MotionEvent pmEvt)
{
int[] cViewPosXY = new int[2];
getLocationOnScreen(cViewPosXY);
int cTouchX = cEvt.getRawX() - cViewPosXY[0];
int cTouchY = cEvt.getRawY() - cViewPosXY[1];
}
}
No comments:
Post a Comment