我有扩展View类的MyView类。 MyView应绘制填充三角形。我画了一个三角形,但我不能把它填满。这是我的onDraw()方法:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(android.graphics.Color.BLACK);
canvas.drawPaint(paint);
paint.setStrokeWidth(4);
paint.setColor(android.graphics.Color.RED);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setAntiAlias(true);
Point a = new Point(0, 0);
Point b = new Point(0, 100);
Point c = new Point(87, 50);
Path path = new Path();
path.setFillType(FillType.EVEN_ODD);
path.moveTo(a.x, a.y);
path.lineTo(b.x, b.y);
path.moveTo(b.x, b.y);
path.lineTo(c.x, c.y);
path.moveTo(c.x, c.y);
path.lineTo(a.x, a.y);
path.close();
canvas.drawPath(path, paint);
}
这就是我得到的结果:
答案 0 :(得分:22)
我找到了答案
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(android.graphics.Color.BLACK);
canvas.drawPaint(paint);
paint.setStrokeWidth(4);
paint.setColor(android.graphics.Color.RED);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setAntiAlias(true);
Point a = new Point(0, 0);
Point b = new Point(0, 100);
Point c = new Point(87, 50);
Path path = new Path();
path.setFillType(FillType.EVEN_ODD);
path.lineTo(b.x, b.y);
path.lineTo(c.x, c.y);
path.lineTo(a.x, a.y);
path.close();
canvas.drawPath(path, paint);
}
答案 1 :(得分:1)
这个答案清楚地说明了@Egis在答案中给出的数字来自何处。 (这将绘制一个倒置的等边三角形并用kotlin写成)
@PostMapping(value={"/tesinsert"}, consumes=MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<?> insert(@RequestBody KasusEntity user) throws Exception {
Map result = new HashMap();
userService.insertTabel(user);
return new ResponseEntity<>(result, HttpStatus.CREATED);
}
获取高度函数为Pythagoras' Theorem,并且总是会发现等边三角形的高度为其边长的约87%
Gist可以在这里找到,它包含另一个方向的代码
答案 2 :(得分:1)
我最近创建了一个小型演示应用程序,可以绘制各种形状,包括三角形,矩形和螺旋形。这是绘制三角形的代码。有关完整的上下文,请参考项目。在此应用中,用户可以将三角形绘制为单个图形,也可以在滚动时将两个三角形的组合绘制成一个三角形。三角形的增长是通过“三角形相似性”原理完成的,每个较小的三角形最后都类似于较大的三角形。
项目:https://github.com/jdgreene2008/android_custom_views
private void drawTriangleShape(Canvas canvas, RectF bounds,
TriangleInterpolator triangleInterpolator, Paint paint) {
paint.setStyle(Paint.Style.FILL);
float baseInterpolation = triangleInterpolator
.getInterpolatedValues()[TriangleInterpolator.INTERPOLATION_VALUES_BASE];
float altitudeInterpolation = triangleInterpolator
.getInterpolatedValues()[TriangleInterpolator.INTERPOLATION_VALUES_ALTITUDE];
// *** Construct the Left Triangle ** //
// Bottom left vertex
float bottomLeftX = bounds.left;
float bottomLeftY = bounds.bottom;
// Bottom right corner
float bottomRightX = bottomLeftX + baseInterpolation;
float bottomRightY = bounds.bottom;
//Top Vertex
float topX = bottomRightX;
float topY = bottomRightY - altitudeInterpolation;
Path leftTriangle = new Path();
leftTriangle.lineTo(bottomLeftX, bottomLeftY);
leftTriangle.lineTo(bottomRightX, bottomRightY);
leftTriangle.lineTo(topX, topY);
leftTriangle.lineTo(bottomLeftX, bottomLeftY);
canvas.drawPath(leftTriangle, paint);
if (triangleInterpolator.isSymmetric()) {
// *** Construct the Right Triangle ** //
bottomLeftX = bounds.right - baseInterpolation;
bottomLeftY = bounds.bottom;
bottomRightX = bounds.right;
bottomRightY = bounds.bottom;
topX = bottomLeftX;
topY = bottomRightY - altitudeInterpolation;
Path rightTriangle = new Path();
rightTriangle.lineTo(bottomLeftX, bottomLeftY);
rightTriangle.lineTo(bottomRightX, bottomRightY);
rightTriangle.lineTo(topX, topY);
rightTriangle.lineTo(bottomLeftX, bottomLeftY);
canvas.drawPath(rightTriangle, paint);
}
}
答案 3 :(得分:0)
这些链接非常有用。
答案 4 :(得分:0)
我想指出,你永远不应该从onDraw()初始化一个对象,因为它被多次调用并导致性能问题。