如何绘制曲线线 [英] how draw curves lines

查看:187
本文介绍了如何绘制曲线线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请,有人可以帮我在A点(720,1140)中间圆和B点(375,490)之间用红色绘制这条线。

Please, someone could help me draw this line in red color between point A(720,1140) the middle oof circle and point B (375,490)

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


推荐答案

这就是我所做的:


  • 找到两个给定点之间的点

  • 计算两个点之间的90度角

  • 使用之前的度数计算出从中点开始的点X像素。

  • 对这3个点使用 path.cubicTo(同时取负号和正号
    的值来确定直线的弯曲方式。

  • Found the point in between the 2 given points
  • Calculated the angle 90 degrees between the 2 points
  • Calculated the point X pixels from the middle point using the calculated degree from before.
  • Used "path.cubicTo" with these 3 points (Takes both negative and positive values to determine which way the line should curve).

这是我的代码!

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应用程序。

I created a Paint application based on this coding idea.

有关屏幕截图,请参见此处

For screenshot refer here

希望这会有所帮助!

这篇关于如何绘制曲线线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆