android canvas 画线,两边都有指针结束的箭头。谁能帮我解决这个问题。?

标签 android canvas

@Override
public void onDraw(Canvas canvas) {
    /*canvas.drawLine(0, 0, 20, 20, paint);
    canvas.drawLine(200, 0, 0, 200, paint);*/
    /*canvas.drawLine(downxpos, downypos, upxpos, upypos, paint);*/
    for (Line l : lines) {

        canvas.drawLine(l.startX, l.startY, l.stopX, l.stopY, paint);
    }
}

example image

我想像我在 Canvas 中显示的图像那样画线。我可以画线,但不知道如何向其添加箭头。

最佳答案

使用这些方法在两侧绘制箭头。

private void drawArrow1(Canvas canvas, Paint paint) {
    double degree = calculateDegree(x, x1, y, y1);
    float endX1 = (float) (x1 + ((10) * Math.cos(Math.toRadians((degree-30)+90))));
    float endY1 = (float) (y1 + ((10) * Math.sin(Math.toRadians(((degree-30)+90)))));

    float endX2 = (float) (x1 + ((10) * Math.cos(Math.toRadians((degree-60)+180))));
    float endY2 = (float) (y1 + ((10) * Math.sin(Math.toRadians(((degree-60)+180)))));

    canvas.drawLine(x1,y1,endX1,endY1,paint);
    canvas.drawLine(x1, y1, endX2,endY2,paint);
}

private void drawArrow(Canvas canvas, Paint paint) {

    double degree1 = calculateDegree(x1, x, y1, y);
    float endX11 = (float) (x + ((10) * Math.cos(Math.toRadians((degree1-30)+90))));
    float endY11 = (float) (y + ((10) * Math.sin(Math.toRadians(((degree1-30)+90)))));

    float endX22 = (float) (x + ((10) * Math.cos(Math.toRadians((degree1-60)+180))));
    float endY22 = (float) (y + ((10) * Math.sin(Math.toRadians(((degree1-60)+180)))));

    canvas.drawLine(x,y,endX11,endY11,paint);
    canvas.drawLine(x,y,endX22,endY22,paint);
}

public double calculateDegree(float x1, float x2, float y1, float y2) {
    float startRadians = (float) Math.atan((y2 - y1) / (x2 - x1));
    System.out.println("radian=====" + Math.toDegrees(startRadians));
    startRadians += ((x2 >= x1) ? 90 : -90) * Math.PI / 180;
    return Math.toDegrees(startRadians);
}

其中 x,y 是起点,x1,y1 是终点。

关于android canvas 画线,两边都有指针结束的箭头。谁能帮我解决这个问题。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56503674/

相关文章:

php - 在 Android 应用程序中将数据发布到本地主机

java - 如何在SWT中在Canvas周围绘制边框

javascript - 在图像上绘图时的像素问题 - html5 canvas

javascript - 如何在 JavaScript 中使用清晰的矩形函数进行移动

javascript - HTML5 Canvas 创建的 JPEG 是 "corrupt"还是 "damaged"?

javascript - 无法像标准对象一样通过tiles[i-1]访问 "2d"对象的属性(javascript)

Android 下载图片并在 ImageView 中显示黑色阴影

android - 一般来说;我应该对网站进行哪些更改以使其对 ipad/iphone/android 友好

机器人 : get an singleTask activity from background to foreground

java - 当应用程序从应用程序列表中关闭时,WorkManager 1.0.0-alpha11 无法在 API <= 22 上工作