3

对于使用简单 (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,但我迷路了。

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

有人知道这样做的例子吗?我似乎找不到合适的搜索词来找到这样的例子。

4

1 回答 1

1

在我看来,同样的方法适用于简单的 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;
}
于 2011-08-11T19:02:07.883 回答