定位path.lineTo()

时间:2019-03-19 17:41:25

标签: java android canvas

我目前正在Android Studio上学习Canvas,但不确定使用正确的标题,但是我在使用Seekbar拖动红点时如何控制三角形的位置上一无所知。下面是详细信息

下面是我成功绘制三角形的代码。

        @Override
        protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint.setColor(Color.RED);
        canvas.drawRoundRect(100, 300, 900, 700, 50, 50,paint);

        Point pointTop = new Point(250, 400);
        Point pointLeft = new Point(150, 150);
        Point pointRight = new Point(350, 150);


        paint.setColor(Color.YELLOW);
        path.setFillType(Path.FillType.EVEN_ODD);
        path.lineTo(pointTop.x, pointTop.y);
        path.lineTo(pointLeft.x, pointLeft.y);
        path.lineTo(pointRight.x, pointRight.y);
        path.lineTo(pointTop.x, pointTop.y);
        path.close();

        canvas.drawPath(path, paint);
        }

图片结果:

enter image description here

预期结果

注意:只有1个红点和1个三角形。

enter image description here

非常感谢=)

2 个答案:

答案 0 :(得分:0)

先看Node类,然后再看setTranslateX

答案 1 :(得分:0)

在阅读了以下链接的文档并添加代码后,我已经解决了问题

https://developer.android.com/reference/android/graphics/Path

享受:)

        @Override
        protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint.setColor(Color.RED);
        canvas.drawRoundRect(100, 300, 900, 700, 50, 50,paint);

        Point pointTop = new Point(250, 400);
        Point pointLeft = new Point(150, 150);
        Point pointRight = new Point(350, 150);


        paint.setColor(Color.YELLOW);
--------------------- added -------------------------------
        path.moveTo(pointLeft.x, pointLeft.y);
        path.moveTo(pointRight.x, pointRight.y);
        path.moveTo(pointTop.x, pointTop.y);
        path.reset();
--------------------- added -------------------------------
        path.setFillType(Path.FillType.EVEN_ODD);
        path.lineTo(pointTop.x, pointTop.y);
        path.lineTo(pointLeft.x, pointLeft.y);
        path.lineTo(pointRight.x, pointRight.y);
        path.lineTo(pointTop.x, pointTop.y);
        path.close();

        canvas.drawPath(path, paint);
        }