#include "common/image_cache_manager.h"
#include "components/ui_button.h"
#include "components/ui_canvas.h"
+#include "components/ui_canvas_ext.h"
+#include "draw/hw_draw_utils.h"
#include "components/ui_label.h"
#include "components/ui_label_ext.h"
#include "traing/Training_UiConfig.h"
canvas = nullptr;
}
+ if (canvasExt) {
+ // 清空轨迹数据,确保加载新的数据
+ TraingModel::GetInstance()->ClearCanvasData();
+ trackSegments_.clear();
+ delete canvasExt;
+ canvasExt = nullptr;
+ }
+
delete scrollView_;
scrollView_ = nullptr;
}
#if defined(SUPPORT_GNSS)
std::vector<Point> canvasData = TraingModel::GetInstance()->GetCanvasData();
if (canvasData.size() >= 3) {
+ #if 0
+ // 选择使用原有方法还是新方法:
+ // 方法1:使用原有绘制方法(推荐,稳定可靠)
canvas = new UICanvas();
canvas->SetHeight(CANVAS_SIZE);
canvas->SetWidth(CANVAS_SIZE);
canvas->SetPosition(33, 100);
scrollView_->Add(canvas);
GenerateTrackPoints();
- // 有足够数据,绘制轨迹
- DrawTrackSegment(canvas);
+ DrawTrackSegment(canvas); // 使用原有绘制方法
DrawMarkers(canvas);
+ #else
+ // 方法2:使用新绘制方法(使用DrawPolyline接口,需要UICanvasExt)
+ // 如需使用新方法,取消下面注释并注释掉上面的方法1代码
+ canvasExt = new UICanvasExt();
+ canvasExt->SetHeight(CANVAS_SIZE);
+ canvasExt->SetWidth(CANVAS_SIZE);
+ canvasExt->SetPosition(33, 100);
+ scrollView_->Add(canvasExt);
+ GenerateTrackPoints();
+ DrawTrackSegmentWithPolyline(canvasExt); // 使用新绘制方法
+ DrawMarkers(canvasExt);
+ #endif
} else {
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS轨迹数据不足,不绘制轨迹");
}
//////////////////////////////gps轨迹测试/////////////////////////////////////
+//绘制轨迹接口
+int16_t DrawPolyline(UICanvasExt* canvas, const Point *points, uint32_t pointNum, const PaintExt& paint)
+{
+ if (canvas == nullptr || points == nullptr || pointNum <= 1) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "invalid parameters canvas(%p), points(%p), pointNum(%u).",
+ canvas, points, pointNum);
+ return -1;
+ }
+
+ Path* path = new Path();
+ (void)memset_s(path, sizeof(Path), 0, sizeof(Path));
+
+ if (!HWDrawUtils::GetInstance()->AllocBufferForPath(path, pointNum, pointNum * 2)) { // 2: coe
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Alloc buffer for path failed.");
+ delete path;
+ return -1;
+ }
+
+ path->data[0] = points[0].x;
+ path->data[1] = points[0].y;
+ path->cmds[0] = CMD_MOVE_TO;
+ for (uint32_t i = 1; i < pointNum; i++) {
+ path->data[2 * i] = points[i].x; // 2: coe
+ path->data[2 * i + 1] = points[i].y; // 2: coe
+ path->cmds[i] = CMD_LINE_TO;
+ }
+
+ canvas->BeginPath(paint);
+ canvas->SetPathData(pointNum, path->cmds, pointNum * 2, path->data);
+ canvas->DrawPath();
+
+ // 释放Path对象分配的内存
+ HWDrawUtils::GetInstance()->FreeBufferForPath(path);
+ delete path;
+ return 0;
+}
+
std::vector<Marker> markers_ = {{{58, 18}, "起", Color::Green()},
{{299, 146}, "终", Color::Red()}};
markers_[0].pos = canvasData[0];
markers_[1].pos = canvasData.back();
}
+// 原有绘制方法 - 保留作为备选
void TraingEnddataView::DrawTrackSegment(UICanvas *canvas) {
// 绘制网格
// Paint debugPaint;
// canvas->DrawLine({0, i}, {CANVAS_SIZE, i}, debugPaint);
// }
- // 绘制轨迹
+ // 绘制轨迹 - 原有方法
for (const auto &seg : trackSegments_) {
if (seg.points.size() < 3) //至少需要 3 个点位
return;
canvas->DrawPath(paint);
}
}
+
+// 使用DrawPolyline接口的新绘制方法 - 作为备选方案
+void TraingEnddataView::DrawTrackSegmentWithPolyline(UICanvasExt *canvas) {
+ // 绘制网格
+ // Paint debugPaint;
+ // debugPaint.SetStrokeColor(Color::Gray());
+ // for (int i = 0; i < CANVAS_SIZE; i += 5) {
+ // canvas->DrawLine({i, 0}, {i, CANVAS_SIZE}, debugPaint);
+ // canvas->DrawLine({0, i}, {CANVAS_SIZE, i}, debugPaint);
+ // }
+
+ // 绘制轨迹 - 使用DrawPolyline接口
+ for (const auto &seg : trackSegments_) {
+ if (seg.points.size() < 3) //至少需要 3 个点位
+ return;
+
+ // 将Paint转换为PaintExt(PaintExt继承自Paint,可以直接设置属性)
+ PaintExt paintExt;
+ paintExt.SetStyle(Paint::PaintStyle::STROKE_STYLE);
+ paintExt.SetStrokeWidth(8);
+ paintExt.SetStrokeColor(seg.color);
+
+ // 使用DrawPolyline接口一次性绘制所有点
+ DrawPolyline(canvas, seg.points.data(), seg.points.size(), paintExt);
+ }
+}
+// 原有绘制方法 - 使用UICanvas
void TraingEnddataView::DrawMarkers(UICanvas *canvas) {
Paint circlePaint;
circlePaint.SetStyle(Paint::PaintStyle::FILL_STYLE);
}
}
+// 新绘制方法重载 - 使用UICanvasExt
+void TraingEnddataView::DrawMarkers(UICanvasExt *canvas) {
+ PaintExt circlePaint;
+ circlePaint.SetStyle(Paint::PaintStyle::FILL_STYLE);
+
+ PaintExt textPaint;
+ textPaint.SetFillColor(Color::White());
+
+ for (const auto &marker : markers_) {
+ // 绘制标记点圆形
+ circlePaint.SetFillColor(marker.color);
+ canvas->DrawCircle(marker.pos, 12, circlePaint);
+
+ // 绘制文字,调整位置和排版
+ UICanvasExt::FontStyle fontStyle;
+ fontStyle.align = TEXT_ALIGNMENT_LEFT; // 文字居左,避免 RTL 反向
+ fontStyle.direct = TEXT_DIRECT_LTR; // 从左到右排版
+ fontStyle.fontName = DEFAULT_VECTOR_FONT_FILENAME;
+ fontStyle.fontSize = 24; // 适配 300*300 画布
+ fontStyle.letterSpace = 2;
+
+ // 文字位置:圆点右侧偏移,避免覆盖
+ canvas->DrawLabel({marker.pos.x + 18, marker.pos.y - 6}, marker.text, 80,
+ fontStyle, textPaint);
+ }
+}
+
+
+
} // namespace OHOS