触摸在可绘制的getBounds()之内



我有一个类,我设置了这样的界限:

tattoo.setBounds(getWidth() / 2 - zoomController - x, getHeight() / 2 - zoomController - y, getWidth()/ 2 + zoomController - x, getHeight()/ 2 + zoomController - y);
tattoo.draw(c);

当用户在这些边界内单击时,我希望能够执行操作。检查单击是否在边界内的最简单方法是什么?

使用描述可绘制的界限的Rect并在其上调用contains(int x, int y)方法,并通过X和Y触摸坐标。例如:

Rect bounds = tattoo.getBounds();
int x = ...
int y = ...
boolean withinBounds = bounds.contains(x, y);

另外,您可以创建自己的(静态)助手方法;数学确实是基本的。:)

最新更新