gpt4 book ai didi

java - 怎么画曲线

转载 作者:行者123 更新时间:2023-12-01 08:52:24 24 4
gpt4 key购买 nike

请有人帮我在圆中间的 A 点(720,1140)和 B 点(375,490)之间画一条红色的线

enter image description here

 Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager()
.getDefaultDisplay().getWidth(), (int) getWindowManager()
.getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawingImageView.setImageBitmap(bitmap);
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int x = metrics.widthPixels;
int y = metrics.heightPixels;
Paint paint1 = new Paint () ;
paint1.setStrokeWidth(10);
int margin = 100;
int margin1 = 300;
int top = 0 + margin;
int bottom = canvas.getHeight() - margin;
int left = 0 + margin1;
int right = canvas.getWidth() - margin1;
int centerX = x / 2;
int centerY = y / 2;
canvas.drawCircle(x / 2, y / 2, 50, paint1);
canvas.drawLine(centerX, top, centerX, bottom,paint1);
canvas.drawLine(left, centerY, right, centerY,paint1);

最佳答案

这是我所做的:

  • 找到两个给定点之间的点
  • 计算出 2 点之间的 90 度角
  • 使用之前计算的度数计算距离中点 X 像素的点。
  • 将“path.cubicTo”与这 3 个点一起使用(同时取负数和正数值来确定线应弯曲的方式)。

这是我的代码!

public PaintApplication drawCurve(int x1, int y1, int x2, int y2, int curveRadius, int padding, int color, int lineWidth) {

Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(lineWidth);
paint.setColor(ContextCompat.getColor(context, color));

final Path path = new Path();
int midX = x1 + ((x2 - x1) / 2);
int midY = y1 + ((y2 - y1) / 2);
float xDiff = midX - x1;
float yDiff = midY - y1;
double angle = (Math.atan2(yDiff, xDiff) * (180 / Math.PI)) - 90;
double angleRadians = Math.toRadians(angle);
float pointX = (float) (midX + curveRadius * Math.cos(angleRadians));
float pointY = (float) (midY + curveRadius * Math.sin(angleRadians));

path.moveTo(x1, y1);
path.cubicTo(x1,y1,pointX, pointY, x2, y2);
canvas.drawPath(path, paint);

return this;
}

我根据这个编码思想创建了一个 Paint 应用程序。

屏幕截图请参阅here

希望这有帮助!!

关于java - 怎么画曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42304280/

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