如何绘制填充多边形?

And*_*ner 53 android

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

And*_*der 108

Android没有drawPolygon(x_array, y_array, numberofpoints)类似Java 的方便动作.你必须逐步完成制作一个Path对象.例如,要为3D地牢墙制作填充的梯形形状,您可以将所有点放在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);
Run Code Online (Sandbox Code Playgroud)

要为某个深度添加常量线性渐变,可以按如下方式编写代码.注意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);
Run Code Online (Sandbox Code Playgroud)

有关更多选项,请参阅" 绘制"," 路径"和" 画布"文档,例如阵列定义的渐变,添加弧以及在多边形上放置位图.

  • 您可以简单地调用`Path.close()`来自动添加结束线段,而不是使用`Path.lineTo(x0,y0)`. (11认同)

Adr*_*ciu 41

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

Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
Run Code Online (Sandbox Code Playgroud)

然后你可以绘制你想要的任何东西,它将被填充.

canvas.drawCircle(20, 20, 15, paint);
canvas.drawRectangle(60, 20, 15, paint);
Run Code Online (Sandbox Code Playgroud)

等等

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


Nux*_*Nux 12

我喜欢分三步完成......

步骤1.创建一个尖类;-)

/**
 * 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

步骤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);
}
Run Code Online (Sandbox Code Playgroud)

第3步.画画

    drawPoly(canvas, 0xFF5555ee,
            new Point[]{
                new Point(10, 10),
                new Point(15, 10),
                new Point(15, 20)
            });
Run Code Online (Sandbox Code Playgroud)

是的,你可以更有效地做到这一点,但可能没那么可读:-).

  • 这段代码有一些严重的问题:首先在Android中已经有两个`Point`和`PointF`类,所以你真的不需要重新发明自己的类.其次,您确实希望避免在View.draw()方法中分配对象,并且您提供的示例会为单次绘制进行大量分配. (8认同)

luQ*_*luQ 5

绘制具有 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();
}
Run Code Online (Sandbox Code Playgroud)