如何绘制填充多边形?

如何在Android中绘制填充多边形?

您需要将绘画对象设置为FILL

Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); 

然后,你可以绘制任何你想要的,它会被填满。

 canvas.drawCircle(20, 20, 15, paint); canvas.drawRectangle(60, 20, 15, paint); 

等等

对于更复杂的形状,您需要使用PATH对象 。

Android没有像Java这样的方便的drawPolygon(x_array, y_array, numberofpoints)动作。 你必须逐步完成一个Path对象。 例如,要为三维地牢墙做出一个实心的梯形形状,可以将所有的点放在x和y数组中,然后编码如下:

 Paint wallpaint = new Paint(); wallpaint.setColor(Color.GRAY); wallpaint.setStyle(Style.FILL); Path wallpath = new Path(); wallpath.reset(); // only needed when reusing this path for a new build wallpath.moveTo(x[0], y[0]); // used for first point wallpath.lineTo(x[1], y[1]); wallpath.lineTo(x[2], y[2]); wallpath.lineTo(x[3], y[3]); wallpath.lineTo(x[0], y[0]); // there is a setLastPoint action but i found it not to work as expected canvas.drawPath(wallpath, wallpaint); 

要为某个深度添加恒定的线性渐变,您可以编码如下。 注意y [0]用于保持梯度水平:

  wallPaint.reset(); // precaution when resusing Paint object, here shader replaces solid GRAY anyway wallPaint.setShader(new LinearGradient(x[0], y[0], x[1], y[0], Color.GRAY, Color.DKGRAY,TileMode.CLAMP)); canvas.drawPath(wallpath, wallpaint); 

有关更多选项,请参考Paint , Path和Canvas文档,例如数组定义的渐变,添加弧线以及在多边形上放置一个位图。

我喜欢分三步做

第1步。创build一个尖尖的类;-)

 /** * Simple point */ private class Point { public float x = 0; public float y = 0; public Point(float x, float y) { this.x = x; this.y = y; } } 

第2步。添加绘图的方法/函数

 /** * Draw polygon * * @param canvas The canvas to draw on * @param color Integer representing a fill color (see http://developer.android.com/reference/android/graphics/Color.html) * @param points Polygon corner points */ private void drawPoly(Canvas canvas, int color, Point[] points) { // line at minimum... if (points.length < 2) { return; } // paint Paint polyPaint = new Paint(); polyPaint.setColor(color); polyPaint.setStyle(Style.FILL); // path Path polyPath = new Path(); polyPath.moveTo(points[0].x, points[0].y); int i, len; len = points.length; for (i = 0; i < len; i++) { polyPath.lineTo(points[i].x, points[i].y); } polyPath.lineTo(points[0].x, points[0].y); // draw canvas.drawPath(polyPath, polyPaint); } 

第3步。绘制

  drawPoly(canvas, 0xFF5555ee, new Point[]{ new Point(10, 10), new Point(15, 10), new Point(15, 20) }); 

是的,你可以做得更有效率,但可能不太可读:-)。

顺便说一句 – 我发现,一旦你开始创build你的path,path中的任何moveTo命令将意味着该形状是空的。

当你考虑这个问题的时候,Android / Java会让形状变得没有填充,因为moveTo会代表多边形中断。

不过,我已经看到这样的一些教程如何在Androidcanvas中绘制一个实心的三angular形?

在每行之后都有moveTo。 尽pipe这可能会导致多边形不间断,但Android会假定moveTo表示多边形的中断。

老问题,但任何人发现这个伎俩。 如果包含所需多边形的字体作为字形,则可以使用drawText函数绘制多边形。

缺点是你必须提前知道你需要什么样的形状。 如果你事先知道,你可以包括一个不错的形状库,这是好事。 此代码假定您的项目的assets / fonts文件夹中有一个名为shapes的字体。

  TypeFace shapesTypeFace = Typeface.createFromAsset(getAssets(), "fonts/shapes.ttf"); Paint stopSignPaint = new Paint(); stopSignPaint.setColor(Color.RED); //set anti-aliasing so it looks nice stopSignPaint.setAntiAlias(true); stopSignPaint.setTextSize(200); stopSignPaint.setTypeface(shapesTypeFace); //will show up as a box or question mark since //the current display font doesn't have this glyph. //open the shapes font in a tool like Character Map //to copy and paste the glyph into your IDE //saving it as a variable makes it much easier to work with String hexagonGlyph = "" String triangleGlyph = "" ....whatever code you got... //arguments: text, x coordinate, y coordinate, paint canvas.drawText(hexagonGlyph, 200, 100, stopSignPaint); //make it into a go sign stopSignPaint.setColor(Color.Green); canvas.drawText(hexagonGlyph, 200, 100, stopSignPaint); //make a tiny one stopSignPaint.setTextSize(20); stopSignPaint.setColor(Color.RED); canvas.drawText(hexagonGlyph, 200, 100, stopSignPaint); //make a triangle canvas.drawText(triangleGlyph, 200, 100, stopSignPaint); 

用x边和自定义半径绘制多边形:

 private void drawPolygon(Canvas mCanvas, float x, float y, float radius, float sides, float startAngle, boolean anticlockwise, Paint paint) { if (sides < 3) { return; } float a = ((float) Math.PI *2) / sides * (anticlockwise ? -1 : 1); mCanvas.save(); mCanvas.translate(x, y); mCanvas.rotate(startAngle); Path path = new Path(); path.moveTo(radius, 0); for(int i = 1; i < sides; i++) { path.lineTo(radius * (float) Math.cos(a * i), radius * (float) Math.sin(a * i)); } path.close(); mCanvas.drawPath(path, paint); mCanvas.restore(); } 

试试这个,或者看完整的演示

  Paint paint = new Paint(); paint.setColor(Color.parseColor("#BAB399")); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));