android - 检测使用 Rect 变换绘制的位图的触摸

标签 android bitmap touch-event

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

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

我可以detect if the bitmap has been touched .

我正在用以下命令将位图绘制到屏幕上

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

试图融入 screen density因为"This function ignores the density associated with the bitmap. ... so must already have the appropriate scaling factor applied."

我无法检测到对已翻译位图的触摸。我一定需要使用 mScale 进行类似的翻译,但我迷路了。

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

有人知道已经完成此操作的示例吗?我似乎找不到合适的搜索词来找到这样的例子。

最佳答案

在我看来,同样的方法适用于简单的 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;
}

关于android - 检测使用 Rect 变换绘制的位图的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6605602/

相关文章:

android - 如果按钮点击 4 次,我可以显示插页式广告吗?

html - touchcancel 和 touchend 事件有什么区别?

android - 我必须用 GoogleApiClient 替换 Android LocationClient 吗?

java - IllegalArgumentException Surface.nativeLockCanvas,编排器,BufferQueue

android - 在 Google map v2 上将 map 标记移动一定数量的像素

java - 将字节数组转换为位图在 java 中给出 bitmap=null

c++ - 简单的钻头设置和清洁

android - 使用自定义 View 创建小部件

ios - 如何使用 Objective-C 移动多个对象

javascript - 捕获JavaScript事件 "silently"