@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
return true;
case MotionEvent.ACTION_UP: {
ProvinceItem temp = null;
for (ProvinceItem provinceItem : provinceItems) {
if (provinceItem.isOnTouch(event.getX() / scale, event.getY() / scale)) {
temp = provinceItem;
break;
}
}
if (temp != null) {
selectItem = temp;
postInvalidate();
}
}
}
return super.onTouchEvent(event);
}
利用Region的contains方法判断抬起坐标是否在path范围内
代码语言:javascript复制
public boolean isOnTouch(float x, float y) {
Region region = new Region();
RectF rect = new RectF();
path.computeBounds(rect, true);
region.setPath(path, new Region((int) rect.left, (int) rect.top, (int) rect.right, (int) rect.bottom));
return region.contains((int) x, (int) y);
}