Ich habe Klasse MyView, die View-Klasse erweitert. MyView sollte ein gefülltes Dreieck zeichnen. Ich zeichnete ein Dreieck, aber ich kann es nicht füllen. Das ist mein OnDraw() Methode:Wie zeichne gefüllte Dreieck auf Android Canvas
@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);
}
Dies ist, was ich als Ergebnis erhalten:
die erste lineTo sollte moveTo sein. Andernfalls beginnt es mit dem Zeichnen von Punkt (0, 0). In diesem Fall kein Problem, aber ich sah es an, da ich aus der Mitte zeichnen wollte. – lucasjmatias