我可以从以下方式获得触摸的坐标:
event.getX();
event.getY();
但我想从0,0的原点开始。
因此,我可以触摸屏幕上的任何位置,当我向上拖动手指时,它将以1的增量增加Y,当我向右拖动手指时,X将以1的增量增加。将X减小-1,向下移动将Y减小-1。
有没有一种数学方法可以做到这一点,或者有更好的类或函数?
任何指导将不胜感激!谢谢!
最佳答案
只需使用:
event.getRawX();
event.getRawY();
产生屏幕坐标,
从
0,0
开始(左上角)到
width,height
(右下角)取决于设备,但我认为这对您来说并不新鲜;)
-更新-
我想我误会了你,对不起。
然后试试这个:
int saved_x = -1;
int saved_y = -1;
@Override
public boolean onTouchEvent(MotionEvent event)
{
final int action = event.getAction();
final int pointer_index = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
int pid = event.getPointerId(pointer_index);
int x = (int)event.getX();
int y = (int)event.getY();
switch (action & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
saved_x = x;
saved_y = y;
break;
case MotionEvent.ACTION_MOVE:
int relative_x = x - saved_x;
int relative_y = y - saved_y;
//move right --> relative_x increases by 'm' pixels of movement
//move left --> relative_x decreases by 'm' pixels of movement
//move down --> relative_y increases by 'm' pixels of movement
//move up --> relative_y decreases by 'm' pixels of movement
//use it :D
break;
case MotionEvent.ACTION_UP:
saved_x = -1; //-1 indicates "no finger on screen" -- just a feature :)
saved_y = -1; //-1 indicates "no finger on screen" -- just a feature :)
break;
}
return true;
}
我希望这就是您想要的:)