检测通过矩形变换绘制的位图的触摸

发布于 2024-11-18 14:40:00 字数 1465 浏览 2 评论 0原文

对于使用简单 (x,y) 坐标绘制的位图,

float _x = x - (bitmap.getWidth() / 2);
float _y = y - (bitmap.getHeight() / 2);
canvas.drawBitmap(bitmap, _x, _y, null);

我可以检测位图是否已被感动了

我正在屏幕上绘制一个位图,并

    dest = new Rect(0,0,0,0);
    src = new Rect(0,0,0,0);
    mSpriteHeight = (int) (sprite_pixel_height * mScale + 0.5f);
    mSpriteWidth = (int) (sprite_pixel_width * mScale + 0.5f);
    src.top = 0;
    src.left = 0;
    src.bottom = mSpriteHeight;
    src.right = mSpriteWidth;
    dest.top = (int) (_x * mScale);
    dest.bottom = (int) ((_x + mSpriteHeight) * mScale);
    dest.left = (int) (_y * mScale);
    dest.right = (int) ((_y + mSpriteWidth) * mScale);
    canvas.drawBitmap(bitmap, src, dest, null);

尝试合并 屏幕密度因为"此函数忽略与位图相关的密度...因此必须已经应用了适当的缩放因子。"

我无法检测对翻译后的位图的触摸。我必须使用 mScale 进行类似的翻译,但我迷路了。

是否有更好的方法在我原来的 canvas.drawBitmap(bitmap, src, dest, null); 中定义 src 和 dest

有人知道这样做的例子吗?我似乎无法找到正确的搜索词来找到这样的示例。

For Bitmaps drawn with simple (x,y) coordinates,

float _x = x - (bitmap.getWidth() / 2);
float _y = y - (bitmap.getHeight() / 2);
canvas.drawBitmap(bitmap, _x, _y, null);

I can detect if the bitmap has been touched.

I'm drawing a bitmap to the screen with

    dest = new Rect(0,0,0,0);
    src = new Rect(0,0,0,0);
    mSpriteHeight = (int) (sprite_pixel_height * mScale + 0.5f);
    mSpriteWidth = (int) (sprite_pixel_width * mScale + 0.5f);
    src.top = 0;
    src.left = 0;
    src.bottom = mSpriteHeight;
    src.right = mSpriteWidth;
    dest.top = (int) (_x * mScale);
    dest.bottom = (int) ((_x + mSpriteHeight) * mScale);
    dest.left = (int) (_y * mScale);
    dest.right = (int) ((_y + mSpriteWidth) * mScale);
    canvas.drawBitmap(bitmap, src, dest, null);

trying to incorporate the
screen density because "This function ignores the density associated with the bitmap. ... so must already have the appropriate scaling factor applied."

I haven't been able to detect touches to the translated bitmaps. I must need to do a similar translation using mScale, but I'm lost.

Is there a better way to define the src and dest in my original canvas.drawBitmap(bitmap, src, dest, null);?

Anyone know an example where this has been done? I can't seem to find the right search terms to find such an example.

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

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

发布评论

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

评论(1

栀梦 2024-11-25 14:40:00

在我看来,同样的方法适用于简单的 x,y 坐标。你只需要使用 Rect dest 的坐标和大小来计算位图是否被触摸。

换句话说,你需要做这样的事情:

public boolean picked(Rect dest, int touchX, int touchY) {
     if(touchX > dest.left && touchX < dest.left + dest.width() &&
        touchY > dest.top && touchY < dest.top + dest.height())
            return true;
     return false;
}

It seems to me that the same method applies as for simple x,y coordinates. You simply need to use the coordinates and size of Rect dest to calculate whether the bitmap has been touched.

In other words, you need to do something like this:

public boolean picked(Rect dest, int touchX, int touchY) {
     if(touchX > dest.left && touchX < dest.left + dest.width() &&
        touchY > dest.top && touchY < dest.top + dest.height())
            return true;
     return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文