I have a bitmap image that I’m trying to do a hit test on. The hit test works if its just a normal bitmap. But I need to rotate and scale the bitmap and I just can’t seem to figure out the hit test properly.
x and y here are the cursor x and y. I need to check if the cursor (finger press) was clicked inside the manipulated bitmap. The scale seems to work fine, but the rotation doesn’t seem to take affect.
float[] pts = new float[4];
float left = m.getX();
float top = m.getY();
float right = left + mBitmaps.get(i).getWidth();
float bottom = top + mBitmaps.get(i).getHeight();
pts[0] = left;
pts[1] = top;
pts[2] = right;
pts[3] = bottom;
float midx = left + mBitmaps.get(i).getWidth()/2;
float midy = top + mBitmaps.get(i).getHeight()/2;
Matrix matrix = new Matrix();
matrix.setRotate(m.getRotation(), midx, midy);
matrix.setScale(m.getSize(), m.getSize(), midx, midy);
matrix.mapPoints(pts);
if(x >= pts[0] && x <= pts[2] && y >= pts[1] && y <= pts[3])
{
return i;
}
Your test fails because after rotation the rectangle is no longer aligned to the coordinate axes.
A trick you can do is to transform the cursor position back with the inverse transformation matrix and then compare the transformed position with the original rectangle.