如何在Android Canvas上绘制填充三角形

时间:2020-12-17 21:19:30

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:

我有扩展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);
}

This is what I get as a result:

这就是我得到的结果:

如何在Android Canvas上绘制填充三角形

4 个解决方案

#1


21  

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

#2


0  

these links are very usefull.

这些链接非常有用。

  1. https://github.com/swapgo20/Android-Hand-Drawing
  2. https://github.com/swapgo20/Android-Hand-Drawing
  3. https://github.com/codepath/android_guides/wiki/Basic-Painting-with-Views
  4. https://github.com/codepath/android_guides/wiki/Basic-Painting-with-Views
  5. https://github.com/Korilakkuma/CanvasView
  6. https://github.com/Korilakkuma/CanvasView

#3


0  

I would like to point out that you should never initiialize an object from onDraw() as it gets called multiple times and leads to performance problems.

我想指出,你永远不应该从onDraw()初始化一个对象,因为它被多次调用并导致性能问题。

#4


0  

This answer provides a bit of clarity on where the numbers given in the answer by @Egis come from. (this will draw an upside down equilateral triangle and is written in kotlin)

这个答案清楚地说明了@Egis在答案中给出的数字来自何处。 (这将绘制一个倒置的等边三角形并用kotlin写成)

class TriangleView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {

    val paint = Paint()
    val path = Path()

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas ?: return
        canvas.drawPath(configurePath(canvas.width.toFloat(), path), configurePaint(paint))
    }

    fun getHeight(width: Double): Float {
        return Math.sqrt((Math.pow(width, 2.0) - Math.pow((width / 2), 2.0))).toFloat()
    }

    fun configurePaint(paint: Paint): Paint {
        paint.color = android.graphics.Color.WHITE
        paint.isAntiAlias = true

        return paint
    }

    fun configurePath(width: Float, path: Path): Path {
        path.lineTo((width / 2f), getHeight(width.toDouble()))
        path.lineTo(width, 0F)
        path.lineTo(0f, 0f)

        return path
    }
}

The get height function is Pythagoras' Theorem and will always find the height of an equilateral triangle to be ~87% of its side length

获取高度函数是毕达哥拉斯定理,并且总是会发现等边三角形的高度约为其边长的87%

Gist can be found here, it contains code for the other direction

Gist可以在这里找到,它包含另一个方向的代码

#1


21  

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

#2


0  

these links are very usefull.

这些链接非常有用。

  1. https://github.com/swapgo20/Android-Hand-Drawing
  2. https://github.com/swapgo20/Android-Hand-Drawing
  3. https://github.com/codepath/android_guides/wiki/Basic-Painting-with-Views
  4. https://github.com/codepath/android_guides/wiki/Basic-Painting-with-Views
  5. https://github.com/Korilakkuma/CanvasView
  6. https://github.com/Korilakkuma/CanvasView

#3


0  

I would like to point out that you should never initiialize an object from onDraw() as it gets called multiple times and leads to performance problems.

我想指出,你永远不应该从onDraw()初始化一个对象,因为它被多次调用并导致性能问题。

#4


0  

This answer provides a bit of clarity on where the numbers given in the answer by @Egis come from. (this will draw an upside down equilateral triangle and is written in kotlin)

这个答案清楚地说明了@Egis在答案中给出的数字来自何处。 (这将绘制一个倒置的等边三角形并用kotlin写成)

class TriangleView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {

    val paint = Paint()
    val path = Path()

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas ?: return
        canvas.drawPath(configurePath(canvas.width.toFloat(), path), configurePaint(paint))
    }

    fun getHeight(width: Double): Float {
        return Math.sqrt((Math.pow(width, 2.0) - Math.pow((width / 2), 2.0))).toFloat()
    }

    fun configurePaint(paint: Paint): Paint {
        paint.color = android.graphics.Color.WHITE
        paint.isAntiAlias = true

        return paint
    }

    fun configurePath(width: Float, path: Path): Path {
        path.lineTo((width / 2f), getHeight(width.toDouble()))
        path.lineTo(width, 0F)
        path.lineTo(0f, 0f)

        return path
    }
}

The get height function is Pythagoras' Theorem and will always find the height of an equilateral triangle to be ~87% of its side length

获取高度函数是毕达哥拉斯定理,并且总是会发现等边三角形的高度约为其边长的87%

Gist can be found here, it contains code for the other direction

Gist可以在这里找到,它包含另一个方向的代码