在Android Canvas多边形上单击
作者:互联网
我在Canvas上有一些多边形.
我怎么能听多边点击任何一个?
我有一个方法
public void drawPolygon(ChartPolygon polygon) {
List<Point> points = polygon.getPoints();
int size = points.size();
if (size < 2) {
return;
}
Paint polyPaint = new Paint();
polyPaint.setColor(polygon.getColor());
polyPaint.setStyle(Style.FILL);
Path polyPath = new Path();
polyPath.moveTo(points.get(0).getX(), points.get(0).getY());
for (Point point : points) {
polyPath.lineTo(point.getX(), point.getY());
}
canvas.drawPath(polyPath, polyPaint);
}
解决方法:
我会保留一份区域清单.每当绘制新的多边形时,使用与绘制相同的路径来定义新的区域:
Region r = new Region();
r.setPath(path, clip);
regionList.add(r);
剪辑可以定义,例如通过
Region clip = new Region(0, 0, canvas.getWidth(), canvas.getHeight());
最后,当您通过regionList检测onTouchListener循环中的触摸并检查时,触摸点(由x和y给出)是否在该区域内:
for (int i = 0; i < regionList.size(); i++) {
Region r = regionList.get(i);
if (r.contains(x,y) {
selectedRegionIndex = i;
break;
}
}
标签:android,polygon,onclick,polygons,android-canvas 来源: https://codeday.me/bug/20190831/1774888.html