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)
有关更多选项,请参阅" 绘制"," 路径"和" 画布"文档,例如阵列定义的渐变,添加弧以及在多边形上放置位图.
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)
是的,你可以更有效地做到这一点,但可能没那么可读:-).
绘制具有 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)
归档时间: |
|
查看次数: |
70494 次 |
最近记录: |