Android does not have the convenient drawPolygon(x_array, y_array, numberofpoints) action drawPolygon(x_array, y_array, numberofpoints) such as Java. You have to go through the creation of the Path object by points. For example, to create a filled trapezoid shape for a 3D dungeon wall, you can put all of your points into x and y arrays, and then encode as follows:
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);
To add a constant linear gradient for a certain depth, you can code as follows. Note that y [0] is used twice to maintain the horizontal gradient:
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);
See Paint , Path, and Canvas for more options, such as gradients defined by an array, adding arcs, and setting a bitmap over your polygon.
Androidcoder Jun 04 2018-12-12T00: 00Z
source share