1

我可以从以下位置获得触摸的坐标:

event.getX();
event.getY();

但我想从 0,0 的原点开始。

所以我可以触摸屏幕上的任何地方,当我向上拖动手指时,Y 将增加 1,当我向右拖动手指时,X 将增加 1。向左移动将将 X 减小 -1,向下移动将使 Y 减小 -1。

有没有一种数学方法可以做到这一点,或者更好的是一个类或函数?

任何指导将不胜感激!谢谢!

4

1 回答 1

3

只需使用:

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;
}

我希望这就是你现在想要的:)

于 2013-06-04T18:10:23.613 回答