获取触摸事件相对于可滚动地图的坐标

发布于 2024-10-30 14:34:49 字数 1483 浏览 0 评论 0原文

有没有办法获取可滚动地图上触摸事件的坐标?

这意味着当我触摸屏幕上的一点时,getX()getY()返回各自的值,然后当我滚动地图时,返回的坐标应该是相对的到地图,而不是屏幕。
例如。我有一个 750x750 的背景地图,我的设备屏幕尺寸是 480x800。
当我第一次触摸时,假设返回的坐标是(100, 200)。现在,当我滚动地图并触摸其他地方时,我得到的坐标为 200, 200。
我想获取相对于地图而不是屏幕的坐标。

我很长一段时间以来一直试图解决这个问题,但在网络和其他网站上进行搜索却徒劳无功。

请帮忙。
提前致谢
\m/

我需要坐标,因为我正在开发一个游戏,其中我有一张大地图和上面放置的物体。当我滚动地图时,我需要对象在同一位置移动。 这是我的代码:

@Override
public boolean onTouchEvent(MotionEvent ev) {

    switch (ev.getAction()) {

    case MotionEvent.ACTION_DOWN:
         touchStart.set(ev.getX(), ev.getY());
     x = ev.getX();
     y = ev.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        newX = ev.getX() - touchStart.x + prevPicStart.x;
        newY = ev.getY() - touchStart.y + prevPicStart.y;
        if ((newX <= 0 && newX > 0 - mBG.getWidth() + Craz.DISP_WIDTH)) {
            picStart.x = newX;
          }
        if ((newY <= 0 && newY > 0 - mBG.getHeight() + Craz.DISP_HEIGHT)) {
            picStart.y = newY;

        }
        invalidate();
        break;

    case MotionEvent.ACTION_UP:

        prevPicStart.x = picStart.x;
        prevPicStart.y = picStart.y;

        break;
    }
    return true;
}

@Override
protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    canvas.drawBitmap(mBG, picStart.x, picStart.y, paint);

    canvas.translate(picStart.x, picStart.y);

    mBDoor.draw(canvas);
}

Is there a way to get the coordinates of a touch event on a scrollable map?

Meaning when I touch at one point on the screen, getX() and getY() return the respective values and then when I scroll the map, the coordinates returned should be relative to the map, not the screen.
eg. I have a 750x750 background map, and my device screen size is 480x800.
when I first touch, say the coordinates returned are (100, 200). now when I scroll the map and touch somewhere else, I get the coordinates as 200, 200.
I want to get the coordinates with respect to the map and not the screen.

I've been trying to figure this out for a long time and have scoured the net and other sites in vain.

please help.
thanks in advance
\m/

i need the coordinates coz i'm developing a game in which i have a large map and objects placed on it. when i scroll the map, i need the objects to move along with in the same position.
here is my code:

@Override
public boolean onTouchEvent(MotionEvent ev) {

    switch (ev.getAction()) {

    case MotionEvent.ACTION_DOWN:
         touchStart.set(ev.getX(), ev.getY());
     x = ev.getX();
     y = ev.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        newX = ev.getX() - touchStart.x + prevPicStart.x;
        newY = ev.getY() - touchStart.y + prevPicStart.y;
        if ((newX <= 0 && newX > 0 - mBG.getWidth() + Craz.DISP_WIDTH)) {
            picStart.x = newX;
          }
        if ((newY <= 0 && newY > 0 - mBG.getHeight() + Craz.DISP_HEIGHT)) {
            picStart.y = newY;

        }
        invalidate();
        break;

    case MotionEvent.ACTION_UP:

        prevPicStart.x = picStart.x;
        prevPicStart.y = picStart.y;

        break;
    }
    return true;
}

@Override
protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    canvas.drawBitmap(mBG, picStart.x, picStart.y, paint);

    canvas.translate(picStart.x, picStart.y);

    mBDoor.draw(canvas);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的温暖 2024-11-06 14:34:49

这很容易。当你滚动地图时,你会有一个偏移量。如果向右滚动(背景将向左移动),则偏移量将为负。比方说,x 上的偏移量为 -50,然后单击屏幕坐标 100,您只需进行数学计算:

mapCoordX = screenX - offsetX; // makes: 100 - (-50) = 150

只需查看 X 坐标,对于 Y 来说,它应该是相同的。

我写了一个关于地图的教程平铺并在其上滚动。也许你也看一下。

伪代码!

for (int id = 0; id < mapSize; id++) {
  Tile tile = new Tile(id);
  startX = id * tileWidth;
  startY = id % rowLenght + id / rowLenght;
  tile.setBackground(createCroppedBitmap(background, startX, startY));
}

Its pretty easy. When you scroll your map, you have an offset. If you scroll to the right (the background will move to the left), your offset will be negative. Lets say, you have an offset on x of -50, and you click the screen coordinates 100 you simply do the math:

mapCoordX = screenX - offsetX; // makes: 100 - (-50) = 150

just looking at the X coordinate, for Y it should be the same.

I have written a tutorial about a map of tiles and scrolling over it. Maybe you take a look at it, too.

Pseudocode!

for (int id = 0; id < mapSize; id++) {
  Tile tile = new Tile(id);
  startX = id * tileWidth;
  startY = id % rowLenght + id / rowLenght;
  tile.setBackground(createCroppedBitmap(background, startX, startY));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文