如何在Android中绘制填充多边形?
最佳答案
Android 没有像 Java 那样方便的 drawPolygon(x_array, y_array, numberofpoints)
Action 。您必须逐点制作 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);
要为某个深度添加恒定的线性渐变,您可以编写如下代码。注意 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有关更多选项的文档,例如数组定义的渐变、添加弧线以及在多边形上放置位图。
关于android - 如何绘制填充多边形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2047573/