开发者

detecting touch for bitmap that's been drawn with Rect transformations

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 facto开发者_JAVA百科r 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.


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜