gpt4 book ai didi

java - 定位 path.lineTo()

转载 作者:行者123 更新时间:2023-11-30 12:06:56 24 4
gpt4 key购买 nike

我目前正在 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

非常感谢=)

最佳答案

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

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

关于java - 定位 path.lineTo(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55247016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com