【问题标题】:detecting touch for bitmap that's been drawn with Rect transformations检测使用 Rect 转换绘制的位图的触摸
【发布时间】:2011-09-30 03:22:58
【问题描述】:

对于使用简单 (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

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

【问题讨论】:

    标签: android bitmap touch-event


    【解决方案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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多