android - 使用角度在画布中绘制箭头
问题描述
我尝试使用以下代码,但此代码未按预期工作。其实我很犹豫是否要为这个简单的解决方案寻求帮助,但是我浪费了很多时间,终于来到了这里。
deltaX = bounds.right - bounds.left;
deltaY = bounds.bottom - bounds.top;
double distance = Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));
float arrowLength= (float) (distance / 3);
float lineAngle = (float) Math.atan2(deltaY, deltaX);
float angle = (float) Math.toRadians(20);
float sinValue = (float) Math.sin(lineAngle - angle);
point_x_1 = bounds.left - 20 * sinValue;
point_y_1 = (float) (bounds.bottom - 0.5 * arrowLength* Math.cos(lineAngle - angle));
angle = (float) Math.toRadians(60);
sinValue = (float) Math.sin(lineAngle + angle);
point_x_3 = bounds.left + 20 * sinValue;
point_y_3 = (float) (bounds.bottom + arrowLength* Math.cos(lineAngle + angle));
path.moveTo(bounds.right, bounds.top);
path.lineTo(bounds.left, bounds.bottom);
path.moveTo(point_x_1, point_y_1);
path.lineTo(bounds.left, bounds.bottom);
path.lineTo(point_x_3, point_y_3);
注意:我有四个方向,每个方向都会出现在不同的场景中。
enum PathDirection {
TopLeftToBottomRight,
TopRightToBottomLeft,
BottomLeftToTopRight,
BottomRightToTopLeft
}
上面的代码我尝试了 TopRightToBottomLeft。
样本输出
图1: RectF 值:[180.0,560.0][820.0,740.0]
图2: RectF 值:[240.0,480.0][640.0,980.0]
更新
path.reset();
canvas.save();
canvas.translate(200, 200);
float direction = (float) Math.atan2(400 - 200, 400 - 200);
canvas.rotate(direction);
path.moveTo(0, 0);
float distance = (float) Math.sqrt(200 * 200 + 200 * 200);
path.lineTo(distance, 0);
float x1 = distance - (distance * 20 / 100);
float y1 = -(distance * 15 / 100);
path.moveTo(x1, y1);
path.lineTo(distance, 0);
x1 = distance - (distance * 20 / 100);
y1 = (distance * 15 / 100);
path.lineTo(x1, y1);
canvas.drawPath(path, mPaint);
canvas.restore();
我用这段代码从位置画线200, 200
到300, 300
. 但这从0, 0
到划线distance
。
截屏
解决方案
在pskink的帮助下,我找到了解决方案,示例代码,它用箭头从200, 200
到画线400, 400
path.reset();
RectF rectF = new RectF(200, 200, 400, 400);
canvas.save();
canvas.translate(rectF.left, rectF.top);
float direction = (float) Math.atan2(rectF.bottom - rectF.top, rectF.right - rectF.left);
float degree = (float) Math.toDegrees(direction);
canvas.rotate(degree);
canvas.drawColor(Color.parseColor("#E3F2FD"));
path.moveTo(0, 0);
float x = rectF.right - rectF.left;
float y = rectF.bottom - rectF.top;
float distance = (float) Math.sqrt(x * x + y * y);
path.lineTo(distance, 0);
float x1 = distance - (distance * 20 / 100);
float y1 = -(distance * 15 / 100);
path.moveTo(x1, y1);
path.lineTo(distance, 0);
float x2 = distance - (distance * 20 / 100);
float y2 = (distance * 15 / 100);
path.lineTo(x2, y2);
canvas.drawPath(path, mPaint);
canvas.restore();
SC:
注意如果您不想旋转画布,可以使用Helder Sepulveda的答案,它也可以按预期工作。
推荐阅读
- php - Laravel 5.6 - 找不到路线
- elasticsearch - 是否必须为我打算使用 ElasticSearch 搜索的所有索引创建 POCO 对象?
- php - 根据 const 和字符串长度打印数据特定次数
- angular - 使用 Moment.js 自动更新相对时间的显示
- .htaccess - htaccess php_value auto_prepend_file 给了我内部 500 错误
- python - python中的双重前瞻断言
- docker - 部署 Azure 容器实例的奇怪错误
- javascript - Html 模式文本输入验证 ng-pattern="/^-?[0-99]+(.[0-99]{0,2})?$/"
- enums - 在处理 switch case 语句时使用枚举
- visual-studio - 如何在最近的 Visual Studio 中运行旧的 VB 宏?