如何在Androidcanvas中绘制一个实心的三angular形?

所以我使用下面的代码在我的绘图方法中绘制了android地图中的这个三angular形:

paint.setARGB(255, 153, 29, 29); paint.setStyle(Paint.Style.FILL_AND_STROKE); paint.setAntiAlias(true); Path path = new Path(); path.moveTo(point1_returned.x, point1_returned.y); path.lineTo(point2_returned.x, point2_returned.y); path.moveTo(point2_returned.x, point2_returned.y); path.lineTo(point3_returned.x, point3_returned.y); path.moveTo(point3_returned.x, point3_returned.y); path.lineTo(point1_returned.x, point1_returned.y); path.close(); canvas.drawPath(path, paint); 

pointX_returned是我从字段中获得的坐标。 他们基本上是纬度和经度。 结果是一个很好的三angular形,但内幕是空的,所以我可以看到地图。 有办法填补它以某种方式?

您可能需要执行以下操作:

 Paint red = new Paint(); red.setColor(android.graphics.Color.RED); red.setStyle(Paint.Style.FILL); 

并使用这种颜色为您的path,而不是你的ARGB。 确保你的path的最后一点在第一个点上结束,这也是有道理的。

告诉我,如果它的工作请!

好的,我已经做到了。 我分享这个代码,以防别人需要它:

 super.draw(canvas, mapView, true); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setStrokeWidth(2); paint.setColor(android.graphics.Color.RED); paint.setStyle(Paint.Style.FILL_AND_STROKE); paint.setAntiAlias(true); Point point1_draw = new Point(); Point point2_draw = new Point(); Point point3_draw = new Point(); mapView.getProjection().toPixels(point1, point1_draw); mapView.getProjection().toPixels(point2, point2_draw); mapView.getProjection().toPixels(point3, point3_draw); Path path = new Path(); path.setFillType(Path.FillType.EVEN_ODD); path.moveTo(point1_draw.x,point1_draw.y); path.lineTo(point2_draw.x,point2_draw.y); path.lineTo(point3_draw.x,point3_draw.y); path.lineTo(point1_draw.x,point1_draw.y); path.close(); canvas.drawPath(path, paint); //canvas.drawLine(point1_draw.x,point1_draw.y,point2_draw.x,point2_draw.y, paint); return true; 

感谢提示尼古拉斯!

你也可以使用vertice:

 private static final int verticesColors[] = { Color.LTGRAY, Color.LTGRAY, Color.LTGRAY, 0xFF000000, 0xFF000000, 0xFF000000 }; float verts[] = { point1.x, point1.y, point2.x, point2.y, point3.x, point3.y }; canvas.drawVertices(Canvas.VertexMode.TRIANGLES, verts.length, verts, 0, null, 0, verticesColors, 0, null, 0, 0, new Paint()); 
 private void drawArrows(Point[] point, Canvas canvas, Paint paint) { float [] points = new float[8]; points[0] = point[0].x; points[1] = point[0].y; points[2] = point[1].x; points[3] = point[1].y; points[4] = point[2].x; points[5] = point[2].y; points[6] = point[0].x; points[7] = point[0].y; canvas.drawVertices(VertexMode.TRIANGLES, 8, points, 0, null, 0, null, 0, null, 0, 0, paint); Path path = new Path(); path.moveTo(point[0].x , point[0].y); path.lineTo(point[1].x,point[1].y); path.lineTo(point[2].x,point[2].y); canvas.drawPath(path,paint); } 

在这里输入图像描述

这个函数显示了如何从位图创build一个三angular形。 也就是说,创build三angular形的裁剪图像。 尝试下面的代码或下载演示示例

  public static Bitmap getTriangleBitmap(Bitmap bitmap, int radius) { Bitmap finalBitmap; if (bitmap.getWidth() != radius || bitmap.getHeight() != radius) finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, false); else finalBitmap = bitmap; Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(), finalBitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); Paint paint = new Paint(); final Rect rect = new Rect(0, 0, finalBitmap.getWidth(), finalBitmap.getHeight()); Point point1_draw = new Point(75, 0); Point point2_draw = new Point(0, 180); Point point3_draw = new Point(180, 180); Path path = new Path(); path.moveTo(point1_draw.x, point1_draw.y); path.lineTo(point2_draw.x, point2_draw.y); path.lineTo(point3_draw.x, point3_draw.y); path.lineTo(point1_draw.x, point1_draw.y); path.close(); canvas.drawARGB(0, 0, 0, 0); paint.setColor(Color.parseColor("#BAB399")); canvas.drawPath(path, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(finalBitmap, rect, rect, paint); return output; } 

上面的函数返回在canvas上绘制的三angular形图像。 阅读更多

第一次初始化后,您需要删除path.moveTo。

 Path path = new Path(); path.moveTo(point1_returned.x, point1_returned.y); path.lineTo(point2_returned.x, point2_returned.y); path.lineTo(point3_returned.x, point3_returned.y); path.lineTo(point1_returned.x, point1_returned.y); path.close(); 

使用@Pavel的答案作为指导,如果您没有点,但有开始x,y和高度和宽度,这是一个辅助方法。 也可以绘制倒置/颠倒 – 这对我来说是有用的,因为它被用作垂直条形图的末端。

  private void drawTriangle(int x, int y, int width, int height, boolean inverted, Paint paint, Canvas canvas){ Point p1 = new Point(x,y); int pointX = x + width/2; int pointY = inverted? y + height : y - height; Point p2 = new Point(pointX,pointY); Point p3 = new Point(x+width,y); Path path = new Path(); path.setFillType(Path.FillType.EVEN_ODD); path.moveTo(p1.x,p1.y); path.lineTo(p2.x,p2.y); path.lineTo(p3.x,p3.y); path.close(); canvas.drawPath(path, paint); } 

每行之后不要moveTo() lineTo()

换句话说,除了第一个移除moveTo()之外,

真的,如果我只是复制粘贴OP的代码,并删除不必要的moveTo()调用,它的工作原理。

没有别的事情需要做。


编辑:我知道OP已经发布了他的“最终工作解决scheme”,但他没有说明为什么它的作品。 实际的原因对我来说是相当惊奇的,所以我觉得有必要添加一个答案。