How to draw filled triangle on android Canvas

前端 未结 5 537
鱼传尺愫
鱼传尺愫 2020-12-09 09:12

I have class MyView that extends View class. MyView should draw filled triangle. I drew a triangle but I cannot get it filled. This is my onDraw() method:

@O         


        
5条回答
  •  时光取名叫无心
    2020-12-09 10:07

    I've found the answer

    @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);
    }
    

提交回复
热议问题