【问题标题】:Drawing line between one point and another在一点和另一点之间画线
【发布时间】:2018-08-27 03:07:47
【问题描述】:

我在一个中心按钮和 12 个外部按钮之间有一个自定义线视图,见图。 代码块 1 是我用来获取每个按钮组合的中心点的代码块。 代码块 2 是我的“自定义绘图”,它在每组中心点之间绘制一条线,总共 12 条线。
12 个自定义行都在一个 xml 文件中。 按钮和线条像两个单独的数组一样传递到公共类 Drawline。

我还创建了一个使用 Path 来绘制三角形的类。这是我图形的左上角。

我想要什么。

按钮中心之间的一条线,在第二个端点附近停止。 我希望点之间的线结束,就像您在“脆脆”和“平衡”按钮之间看到的那样。我需要帮助找到如何“获取”这一点的 x,y。我还有什么把箭头放在行尾。

public class Drawline {
    public void drawLines(List<LineView> mlinesToDraw,ArrayList<Button> buttonsbalance, Context
            context,Button btnbase) {
        float centerXOnImage1;
        double centerYOnImage1;
        float centerXOfImageOnScreen1;
        double centerYOfImageOnScreen1;
        float centerXOnImage2;
        float centerYOnImage2;
        float centerXOfImageOnScreen2;
        float centerYOfImageOnScreen2;
        List<LineView> mLine = mlinesToDraw;
        ArrayList<Button> btns = buttonsbalance;
        PointF pointA;
        PointF pointB;

        for (int i = 0; i < mLine.size(); i++) {
            Button button1 = btns.get(i + 1);  //skip btnx0/btnbase
            centerXOnImage1 = button1.getWidth() / 2;
            centerYOnImage1 = (button1.getHeight()) / 2;//-actionBarHeight)/2;
            centerXOfImageOnScreen1 = button1.getLeft()+ centerXOnImage1;
            centerYOfImageOnScreen1 = button1.getTop() + (centerYOnImage1);

            Button button2 = btnbase;
            centerXOnImage2 = button2.getWidth() / 2;
            centerYOnImage2 = (button2.getHeight()) / 2;//-actionBarHeight)/2;
            centerXOfImageOnScreen2 = button2.getLeft() + centerXOnImage2;
            centerYOfImageOnScreen2 = button2.getTop() + (centerYOnImage2);

            pointA = new PointF(centerXOfImageOnScreen1, (float) centerYOfImageOnScreen1);
            pointB = new PointF(centerXOfImageOnScreen2, (float) centerYOfImageOnScreen2);

            mLine.get(i).setPointA(pointA);
            mLine.get(i).setPointB(pointB);
            mLine.get(i).draw();
        }
    }

自定义绘图类。

public class LineView extends View {
   private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
   private PointF pointA,pointB;

//    private void init() {
//        paint.setColor(Color.BLACK);
//    }

    public LineView(Context context) {
        super(context);
     //   init();
    }

    public LineView(Context context, AttributeSet attrs) {
        super(context, attrs);
     //   init();
    }

    public LineView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
       // init();
    }
    @SuppressLint("ResourceAsColor")
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int color = R.color.GradientStart;
        paint.setColor(color);
        paint.setAntiAlias(true);
         paint.setStrokeWidth(6);
         canvas.drawLine(pointA.x, pointA.y, pointB.x, pointB.y, paint);
    }
    public void setPointA(PointF point){
            pointA=point;}
    public void setPointB(PointF point){
        pointB=point;}

    public void draw(){
        invalidate();
        requestLayout();
    }}

我觉得应该有一个更简单的方法来做到这一点,但我还没有找到它。 我很感激任何帮助。 谢谢, 吉姆

【问题讨论】:

    标签: android drawing point


    【解决方案1】:

    因为我现在知道每个外部按钮的角度和到点的半径,所以我能够使用极坐标获得位置。然后我用下面的转换得到了对应的x和y坐标。

    val x = 半径 * Math.cos(angle); val y = 半径 * Math.sin(角度);

    这给了我画线所需的备用端点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      相关资源
      最近更新 更多